Reported October 2026
Snapstack

Basic Calculator II

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

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

The input can hit 200000 characters, so anything that rebuilds or re-scans the string per operator is dead on arrival. That's the quiet filter in the Snap Basic Calculator II question, reported in October 2026. It looks like string parsing, but it's really a stack problem with a precedence twist. You read digits, remember the previous operator, and settle multiplication and division on the spot. If your head goes blank when the OA clock starts, StealthCoder runs invisibly on your desktop as a safety net and hands you the clean single-pass solution.

The problem

Evaluate an arithmetic expression containing non-negative integers, spaces, and the binary operators +, -, *, and /.
Multiplication and division have higher precedence than addition and subtraction. Operators with the same precedence are evaluated from left to right. Integer division truncates toward zero, and the expression contains no parentheses.

Function
calculate(expression: String) → int

Examples
Example 1
expression = "3+2*2"
return = 7
Multiplication is evaluated first, so the result is 3 + 4 = 7.
Example 2
expression = " 3/2 "
return = 1
Integer division truncates 3 / 2 toward zero.
Example 3
expression = " 3+5 / 2 "
return = 5
Division is evaluated before addition, producing 3 + 2.

Constraints
1 <= expression.length <= 200000.
After spaces are removed, the expression alternates non-negative base-10 integers and operators from +, -, *, and /.
No divisor is zero.
Every intermediate and final result fits a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to never build a full expression tree or recurse. Walk the string once. Keep a running number, the last operator you saw, and a stack. When you finish a number, apply the last operator: plus pushes the number, minus pushes its negative, star pops and multiplies, slash pops and divides. At the end, sum the stack. You can drop the stack and keep just a running total plus a last-term value for O(1) space. The common pitfall is truncation. Division must truncate toward zero, so in languages where integer division floors, negative values break. Use trunc logic or int(a / b) carefully. Other traps: spaces anywhere, multi-digit numbers, and forgetting to flush the final number after the loop ends. If you freeze in the live OA, StealthCoder is the hedge that reads the prompt and gives you the working code.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Basic Calculator II 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

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

Snap reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Basic Calculator II FAQ

How hard is the Snap Basic Calculator II problem really?+

Medium. The idea is short once you've seen it, but the edge cases bite: spaces, multi-digit numbers, flushing the last number, and truncation toward zero. Most failures come from those details, not from the algorithm itself.

What's the trick to handling operator precedence?+

Defer addition and subtraction by pushing signed numbers onto a stack. Resolve multiplication and division immediately against the top of the stack. At the end, sum everything. This gives correct precedence in one pass with no parentheses logic.

Why does the 200000 length constraint matter?+

It rules out quadratic approaches like repeatedly slicing the string or re-evaluating after each operator replacement. You need a single left-to-right pass, O(n) time. Avoid string concatenation in loops too, since that can get costly in some languages.

How should I handle integer division with negatives?+

The spec says truncate toward zero. In Python, // floors, so use int(a / b) or manual sign handling. In Java and C++, plain integer division already truncates toward zero. Test a case like a negative stack top divided by 2.

How do I prepare for this in 48 hours?+

Write the stack version from scratch twice, then the O(1) space version. Test with spaces, a single number, and chains like 2*3*4 and 10-2*3. Also be ready for a follow-up with parentheses, which is the harder sibling problem.

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

OA at Snap?
Invisible during screen share
Get it