Reported May 2026
Googlestack

Evaluate a Nested Math Expression

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The whole problem hinges on a stack. Google's May 2026 OA reportedly includes "Evaluate a Nested Math Expression", where add(x,y) and sub(x,y) nest inside each other as deep as the string allows. It looks like parsing trivia, but the 2 * 10^5 length cap is the real catch. If your plan is plain recursion, a deeply nested input can blow the call stack. You've got a day or two, so lock in the iterative approach now. And if you blank mid-assessment, StealthCoder runs invisibly on screen and can hand you the working solution as a safety net.

The problem

You are given a string s representing a nested arithmetic expression.
The expression uses function-call syntax:
add(x, y) evaluates to x + y.
sub(x, y) evaluates to x - y.
Each argument x or y is either an integer or another nested add/sub expression. The expression is guaranteed to be syntactically valid.
Return the integer value of the expression.

Function
evaluateExpression(s: String) → int

Examples
Example 1
s = "add(1,sub(1,0))"
return = 2
sub(1,0) = 1, then add(1,1) = 2.
Example 2
s = "add(sub(5,2),sub(1,4))"
return = 0
sub(5,2) = 3 and sub(1,4) = -3, so the result is 0.
Example 3
s = "sub(add(7,8),sub(3,1))"
return = 13

Constraints
1 <= s.length <= 2 * 10^5
Integer literals fit in a 32-bit signed integer.
The input expression is valid and contains only add, sub, integer literals, parentheses, commas, and optional spaces.
The final result fits in a 32-bit signed integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to treat this as a stack-based evaluation, not a grammar problem. Scan left to right. When you hit a function name, push an operator marker. When you hit a number, push it as a value. On a closing parenthesis, pop the two operands and the operator, compute, and push the result back. A comma and spaces just get skipped. The common pitfall is recursive descent in a language with a shallow recursion limit, since 2 * 10^5 characters can nest tens of thousands of levels. The second pitfall is negative numbers. Intermediate results can go negative, and literals may carry a minus sign, so parse the sign with the digits. Watch operand order on sub, because x minus y isn't commutative and a stack pops in reverse. If the live OA has you fumbling that order, StealthCoder is the hedge that keeps you moving.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Evaluate a Nested Math Expression cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Evaluate a Nested Math Expression FAQ

How hard is the Google nested expression problem really?+

Easy to medium. The logic is short, but the input size punishes naive recursion. If you know a stack-based parse, it's about 30 lines. Most failures come from operand order on sub and from mishandling negative literals, not from the core idea.

What's the trick to solving it?+

Use an explicit stack. Push values and operators as you scan. On each closing parenthesis, pop two operands and the operator, compute the result, and push it back. The final stack entry is your answer. Skip commas and spaces entirely.

Can I just use recursion?+

You can, but it's risky. With a string up to 2 * 10^5 characters, nesting can get very deep and overflow the call stack in many languages. Iterative with a stack is safer. If you do recurse, keep a single shared index pointer.

How do I handle negative numbers and subtraction order?+

Parse a leading minus as part of the integer literal. For sub, pop the second operand first, then the first, and compute first minus second. Reversing them is the most common bug, and Example 2 with sub(1,4) catches it fast.

How do I prepare in 48 hours?+

Write the stack solution from scratch twice, then test it on the three given examples plus a deeply nested case and a negative literal. Also try a variant with mul or nested spaces. Aim to explain the pop order out loud, since that's where bugs hide.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it