Parenthesized String Calculator
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Meta OA, reported in September 2026, is parsing digits one character at a time and forgetting that 42 is a single number. Parenthesized String Calculator looks like a basic string problem. It isn't forgiving. You get +, -, parentheses, spaces, and an expression up to 10^5 characters. No multiplication, no unary signs. That's the whole trick: it's a stack problem in disguise. If you blank halfway through the live assessment, StealthCoder runs invisibly as a safety net and shows you the stack approach while you keep typing.
The problem
Evaluate a valid arithmetic expression containing nonnegative decimal integers, binary + and -, parentheses, and optional spaces. Return the integer result. Every operator has an operand on both sides, parentheses are balanced, and all intermediate values fit signed 32-bit range. Function calculate(expression: String) → int Examples Example 1 expression = "1 + (2 - 3) + 10" return = 10 The parenthesized term equals -1. Example 2 expression = "12-(4+(2-1))" return = 7 The inner expression is 1, so the grouped value is 5. Example 3 expression = "42" return = 42 A single literal evaluates to itself. Constraints 1 <= expression.length <= 10^5. The expression grammar is valid and uses only digits, spaces, +, -, (, and ). Operators are binary; unary signs are not present.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use a single left-to-right pass with a running result, a current sign, and a stack. Read digits into a full number, and when you hit a non-digit, apply the number using the current sign. On '(' push the running result and the current sign onto the stack, then reset result to 0 and sign to +1. On ')' finish the pending number, then pop: result = poppedResult + poppedSign * result. Spaces get skipped. The common pitfall is dropping the last number at the end of the string, or resetting the sign wrong after a parenthesis. Also don't use recursion blindly: depth can reach about 5*10^4, which can overflow the call stack in some languages. An iterative stack is O(n) time and O(n) space. StealthCoder is the hedge if the sign-flip logic slips under live pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Parenthesized String Calculator 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as basic calculator. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Meta's OA.
Meta reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Parenthesized String Calculator FAQ
What's the trick to Parenthesized String Calculator?+
Track a running result and a sign. When you see an open parenthesis, push both onto a stack and reset. When you see a close parenthesis, pop and combine: previous result plus previous sign times the inner result. That handles nesting without any expression-tree parsing.
How hard is this Meta OA question really?+
Medium. There's no operator precedence, which removes the hard part of calculator problems. The difficulty is in details: multi-digit numbers, spaces, and sign handling around nested parentheses. If you've written a stack-based evaluator once, it's about a 20-minute problem.
Should I use recursion or a stack?+
Use an explicit stack. The expression can be 10^5 characters long, so deeply nested parentheses can blow the recursion limit in many languages. The iterative version is the same logic and runs in O(n) time with no stack overflow risk.
What edge cases break most first attempts?+
Forgetting to flush the last number at the end of the string, treating each digit as its own number, and not skipping spaces. Also check a lone literal like 42 and deep nesting like 12-(4+(2-1)). Run those three before submitting.
How do I prepare for this in 48 hours?+
Write the stack solution from scratch twice without notes. Test on the three examples plus nested cases and extra spaces. Then try the variant with multiplication, since similar calculator questions get asked. Focus on the pattern, not memorizing code.