Reported September 2026
Googlestack

Calculator with Parentheses

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

Google's September 2026 OA hands you a calculator that has to handle unary minus in front of a whole parenthesized group, like "-(10 - (3 + 2))" returning -5. That's the detail that trips people. It's a stack or recursive descent parsing problem, with 10^5 characters of input. If you blank on the unary handling mid-assessment, StealthCoder runs invisibly as a safety net and gives you a working solution in real time. But the pattern is learnable in one sitting.

The problem

Evaluate a valid integer expression containing non-negative decimal integers, ASCII spaces, binary +, -, *, and /, unary + and -, and balanced parentheses.
Parentheses have the highest grouping priority, multiplication and division have higher precedence than addition and subtraction, and binary operators with the same precedence are evaluated from left to right. Integer division truncates toward zero, and the expression never divides by zero.
Every parsed integer, intermediate value, and final result fits in a signed 32-bit integer. Return the final result.

Function
calculateExpression(expression: String) → int

Examples
Example 1
expression = "1 + (2 - 3) + 4"
return = 4
The parenthesized subexpression evaluates to -1.
Example 2
expression = "-(10 - (3 + 2))"
return = -5
Unary minus applies to the complete parenthesized expression.

Constraints
1 <= expression.length <= 10^5
The expression is valid and parentheses are balanced.
The final result and every parsed integer fit in signed 32-bit range.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one pass with a stack and a running sign, or recursive descent with three levels: expression, term, factor. The factor level handles unary +/-, integers, and parenthesized groups, so "-(...)" falls out naturally. The stack version keeps a result, a sign, and a current number, pushes both on "(", and pops on ")". Multiplication and division need a last-term trick: keep the pending term and apply * or / to it before adding it to the total. The common pitfall is division. Integer division must truncate toward zero, so in Python don't use // on negatives, use int(a / b) or a manual sign fix. Another pitfall is recursion depth at 10^5 characters with deeply nested parentheses, so prefer an explicit stack. StealthCoder is the hedge if you freeze on the unary or precedence wiring during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Calculator with Parentheses 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as basic calculator iii. If you have time before the OA, drill that.

⏵ The honest play

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

Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Calculator with Parentheses FAQ

What's the trick to the Google Calculator with Parentheses question?+

Treat unary minus as a factor-level concern. Parse a factor as optional sign, then a number or a parenthesized expression. That makes -(10 - (3 + 2)) work without special cases. Then layer terms (* and /) over factors, and expressions (+ and -) over terms.

Should I use recursion or an explicit stack?+

Use an explicit stack if you're worried about depth. With 10^5 characters, nested parentheses could overflow the recursion limit in some languages. Recursive descent is cleaner to write, but a stack with a saved sign and result per level is safer.

How do I handle integer division truncating toward zero?+

Don't rely on floor division for negatives. In Python, -7 // 2 gives -4, but you need -3. Use int(a / b) or compute the absolute quotient and reapply the sign. In C++ and Java, the / operator already truncates toward zero.

How is this different from LeetCode Basic Calculator?+

It adds multiplication and division with precedence, plus unary operators and left-to-right ties. That makes it a blend of Basic Calculator and Basic Calculator II. If you've solved both, you've seen every piece. The work is combining them cleanly.

How do I prepare for this in 48 hours?+

Write the recursive descent version once from scratch, then the stack version. Test with spaces, nested unary like -(-3), and negative division like -7/2. Those three cases catch most bugs. Then you're set for the Google OA.

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