Basic Calculator
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at Motive's Basic Calculator is treating the minus sign like a plain operator and forgetting it flips everything inside the parentheses that follow it. Motive candidates reported this one in June 2023, and it's a classic stack problem dressed up with spaces and unary signs. The input can hit 3 * 10^5 characters, so recursion-heavy or string-slicing approaches get ugly fast. If you've got the OA in a day or two, learn the sign-stack trick below. StealthCoder runs invisibly during the live assessment as a safety net if your mind goes blank on the parentheses logic.
The problem
Given a valid arithmetic expression s, return its evaluated integer value. The expression may contain: Non-negative integer literals. The binary operators + and -. Parentheses ( and ). Spaces. Unary + or - where a signed expression is valid. Integer division is not needed because the expression contains no multiplication or division operators. Function calculate(s: String) → int Examples Example 1 s = "1 + 1" return = 2 The two operands sum to 2. Example 2 s = " 2-1 + 2 " return = 3 Evaluate from left to right: 2 - 1 + 2 = 3. Example 3 s = "(1+(4+5+2)-3)+(6+8)" return = 23 The first parenthesized group evaluates to 9, and 6 + 8 = 14, for a total of 23. Constraints 1 <= s.length <= 3 * 10^5 s is a valid expression containing digits, +, -, (, ), and spaces. Every intermediate and final result fits in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one pass with a running result, a current sign (+1 or -1), and a stack. Read digits into a number. On + or -, add sign * number to result, then set the new sign. On '(', push the current result and the current sign onto the stack, then reset result to 0 and sign to 1. On ')', finish the pending number, then multiply result by the popped sign and add the popped result. The common pitfall is forgetting to flush the last number at the end of the string, or mishandling multi-digit numbers and spaces. Unary minus like "-(2+3)" works naturally because result starts at 0 and sign starts at 1. It's O(n) time and O(n) stack space. If the parenthesis handling slips under the clock, StealthCoder is the hedge during the live OA: it reads the problem and gives you a working solution.
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 Basic 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. 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. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive 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.
Basic Calculator FAQ
How hard is Basic Calculator really?+
It's labeled hard on LeetCode, but the stack solution is short once you see it. The difficulty is bookkeeping: signs, multi-digit numbers, and parentheses. Motive reported it in June 2023. Write it once from memory and it stops feeling hard.
What's the trick to handle parentheses?+
Push the running result and the sign before the '(' onto a stack, then reset both. When you hit ')', multiply the inner result by the popped sign and add the popped result. That handles nested groups and negated groups without recursion.
How do I handle unary minus like -(1+2)?+
Start with result 0 and sign 1. A leading '-' just sets sign to -1 before the '(' is pushed. The stack then carries that negative sign, so the group gets negated on close. No special case needed if you push the sign correctly.
Should I use recursion or a stack?+
Use an explicit stack. With strings up to 3 * 10^5 characters, deeply nested parentheses can blow the recursion depth in some languages. The iterative version is also easier to debug when a test case fails.
How do I prepare in 48 hours?+
Write the sign-stack solution from scratch twice. Test on " 2-1 + 2 ", "(1+(4+5+2)-3)+(6+8)", and "-(2+3)". Check multi-digit numbers and the final number flush. That covers the usual failures for this problem.