Reported June 2024
Navanstack

Basic Calculator

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

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

Most people who fail Navan's Basic Calculator, reported in June 2024, fail on one thing: they forget that a minus sign in front of a parenthesis flips everything inside it. The problem looks easy. Evaluate a string with digits, plus, minus, parentheses and spaces. No multiplication, no division. But the string can run to 300,000 characters, and a sloppy recursive approach or a missed sign will wreck you. This is a stack problem at heart. If you know the sign-stack trick, it's about 20 lines. If you blank during the live OA, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution.

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 to never build a parse tree. Walk the string once. Keep a running result, a current sign (+1 or -1), and a stack. Read digits into a number, and when you hit an operator, fold the number into the result using the current sign. On '(' push the current result and the sign onto the stack, then reset result to 0 and sign to 1. On ')' finish the pending number, multiply the result by the popped sign, and add the popped result. The common pitfall is the trailing number: you must flush it after the loop ends. Others forget multi-digit numbers, or mishandle spaces and unary minus like "-(2+3)". Starting with result 0 and sign 1 handles a leading minus for free. Recursion can blow the stack at 3 * 10^5 characters, so go iterative. If the sign logic slips under pressure, StealthCoder is the hedge on 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 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 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 Navan's OA.

Navan 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

What's the trick to Basic Calculator?+

Use a stack that stores the result and sign before each open parenthesis. Process characters left to right with a running result and sign. On '(' push both and reset. On ')' multiply by the popped sign and add the popped result. One pass, O(n) time.

What's the most common mistake on this problem?+

Not applying the sign before a parenthesis to the whole group. A close second is forgetting to add the last number after the loop ends. Test with "2-(5-6)" and a string that ends in a digit before you submit.

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

Start with result 0 and sign 1. A leading minus sets sign to -1, then the '(' pushes that sign onto the stack. When the group closes, the popped sign flips the inner total. No special unary case needed.

Is recursion okay with a 3 * 10^5 length input?+

Risky. Deeply nested parentheses can hit the recursion limit in many languages. The explicit stack version is safer and just as short. Go iterative so you don't have to think about depth at all.

How do I prepare for this in 48 hours?+

Write the stack solution from memory twice. Then trace three inputs by hand: "1 + 1", " 2-1 + 2 ", and "(1+(4+5+2)-3)+(6+8)". Add edge cases with nested negatives and spaces. If you can explain the sign-flip on ')', you're ready.

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

OA at Navan?
Invisible during screen share
Get it