Arithmetic Expression Evaluator
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that wrecks most solutions on this Microsoft OA, reported September 2026, is the unary minus sitting in front of a parenthesis, like 2 * -(3 + 4). A naive left-to-right parser treats that minus as a binary operator and spits out garbage. This is a classic expression evaluator with multi-digit numbers, spaces, nested parentheses, and truncating division. Stack or recursive descent both work. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you a working parser, so one forgotten sign rule doesn't sink the whole attempt.
The problem
Evaluate a valid arithmetic expression containing multi-digit integers, spaces, parentheses, binary operators +, -, *, and /, and unary + or -. Use standard precedence: unary signs first, then multiplication and division, then addition and subtraction. Operators at the same precedence are left-associative. Integer division truncates toward zero. Return the expression's signed 64-bit integer value. Function evaluateExpression(expression: String) → long Examples Example 1 expression = "-7 + 5 * 8 - 5 / 4 + (5 + 4)" return = 41 Multiplication and division are evaluated before addition and subtraction: -7 + 40 - 1 + 9 = 41. Example 2 expression = "2 * -(3 + 4) + 10 / 3" return = -11 The parenthesized value is negated, and 10 / 3 truncates to 3, giving -14 + 3 = -11. Constraints 1 <= expression.length <= 100000 Parentheses are nested at most 200 levels deep. The expression contains only digits, spaces, parentheses, and the operators +, -, *, and /. The expression is valid, every division has a nonzero divisor, and every intermediate result fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is deciding whether each + or - is binary or unary. Track the previous token: if it's the start, an open paren, or another operator, the sign is unary. The cleanest approach is recursive descent with three levels: expression handles + and -, term handles * and /, factor handles unary signs, numbers, and parenthesized groups. The factor level calling itself on a unary minus is what makes -(3 + 4) work. Pitfalls: division must truncate toward zero, so Python's // is wrong for negatives and you need int(a / b) style logic or manual sign handling. Skip spaces everywhere. Input hits 100000 characters with 200 nesting levels, so recursion depth is fine, but keep it linear. Use 64-bit math. If the parser logic slips under pressure, StealthCoder is the hedge that reads the problem on screen and gives you a correct version live.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Arithmetic Expression Evaluator 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as basic calculator iii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft 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.
Arithmetic Expression Evaluator FAQ
What's the trick to the Microsoft arithmetic expression evaluator?+
Handle unary signs at the lowest level of the grammar. Parse factor as: optional sign, then number or parenthesized expression. Then term loops over * and /, and expression loops over + and -. That structure gives you correct precedence and left-associativity without special cases.
How do I handle integer division truncating toward zero?+
Don't rely on floor division for negatives. -7 / 2 should be -3, not -4. Compute the absolute values, divide, then reapply the sign, or use a language's native truncating division like C++ or Java's /. Test with a negative numerator and a positive divisor.
Stack or recursive descent, which is safer?+
Both pass. Recursive descent is easier to get right with unary operators and nested parentheses, and 200 levels of nesting is well within normal recursion limits. A two-stack shunting-yard approach works too, but unary handling needs extra care.
What edge cases should I test before submitting?+
Test a leading unary minus, a minus directly before a parenthesis, double signs like 3 - -2, spaces everywhere, multi-digit numbers, and negative truncating division like -7 / 2. Also try a single number and a deeply nested expression.
How do I prepare for this in 48 hours?+
Write the three-level recursive descent parser from scratch twice with a tokenizer-free index pointer. Then run the two examples plus your own unary and division edge cases. This problem is a variant of the well-known calculator family, so the structure transfers directly.