Reported September 2026
Amazonstack

Basic Calculator

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

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

The whole Basic Calculator problem comes down to one stack. Amazon candidates reported this OA in September 2026, and it's the classic evaluate-an-expression task with plus, minus, parentheses, spaces and unary signs. No multiplication, no division, so there's no precedence to juggle. That makes it easier than it looks, but the parentheses and signs still trip people who start coding before they pick a plan. If you've got an invite and 48 hours, learn the sign-stack idea below. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.

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: since there's only + and -, parentheses just decide whether a sign gets flipped. Keep a running result, a current sign (1 or -1), and a stack. Scan left to right. On a digit, build the full number and add sign * number to result. On + or -, set sign. On '(', push the current result and the sign onto the stack, then reset result to 0 and sign to 1. On ')', pop: result = poppedResult + poppedSign * result. Skip spaces. The common pitfalls are multi-digit numbers, forgetting to reset after '(', and unary minus like "-(2+3)" or "(-2)", which the reset-to-zero approach handles for free. Don't use recursion on a 3 * 10^5 length string, since deeply nested parentheses can overflow the call stack. The scan is O(n) time and O(n) space. If you blank during the live OA, StealthCoder can hand you this stack template so you can verify it against your own logic.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

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

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Basic Calculator FAQ

How hard is Basic Calculator really?+

It's a LeetCode Hard by label, but without multiplication and division it plays like a medium. The only real idea is the sign stack. If you've seen it once, you can write it in about 15 lines. The risk is sloppy handling of multi-digit numbers and nested parentheses.

What's the trick to handling parentheses?+

Treat '(' as a save point. Push your running result and current sign onto the stack, then restart result at 0 and sign at 1. On ')', pop and combine: previous result plus popped sign times the inner result. No operator precedence is needed.

How do I handle unary minus like -(2+3) or (-2)?+

Start result at 0 and sign at 1. A leading '-' just sets sign to -1 before the next number or parenthesis. After '(' you reset result to 0, so '(-2)' evaluates as 0 + (-1 * 2). No special case is needed.

Can I solve it recursively instead?+

You can, but the input can reach 3 * 10^5 characters, so heavily nested parentheses may blow the call stack in some languages. An explicit stack with a single loop is safer and just as short. Use that one in the assessment.

How do I prepare for this in 48 hours?+

Write the iterative stack solution from scratch twice, then test it on "1 + 1", " 2-1 + 2 ", and "(1+(4+5+2)-3)+(6+8)". Add edge cases with leading negatives and nested groups. Also review the variant with * and /, since it's a common follow-up.

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

OA at Amazon?
Invisible during screen share
Get it