← Questions
Evaluate an arithmetic expression with variables
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round, last asked Sep 2025. It tests parsing, stacks and expressions.
Write an evaluator for integer arithmetic expressions given as strings: non-negative integer literals, spaces, parentheses, unary minus and the binary operators + - * / % ^. Then extend it with named variables whose values come from a map.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
evaluate(s: str, variables: dict[str, int] | None = None) -> intreturns the value of the expression, or raises an error on malformed input.
Rules:
- Precedence from high to low:
^(right-associative), unary minus, then* / %(left-associative), then+ -(left-associative). So-2 ^ 2is-(2 ^ 2). -
/truncates toward zero and%is the matching remainder (it takes the sign of the dividend), soa == (a / b) * b + a % b. - A
-is unary at the start, right after an operator, or right after(. - Arithmetic is on 64-bit signed integers.
- Errors: unbalanced parentheses, an operator with a missing operand, an unknown character, division or modulo by zero, and (for Part 2) a variable with no value.
Example
evaluate("1 + 2 * 3") -> 7
evaluate("2 ^ 3 ^ 2") -> 512 # 2 ^ (3 ^ 2)
evaluate("2 * (3 + 4) ^ 2 - 5") -> 93
evaluate("-(4 - 10) / 4") -> 1 # 6 / 4 truncates
evaluate("-7 / 2") -> -3
evaluate("-7 % 3") -> -1
evaluate("-2 ^ 2") -> -4
evaluate("rate * (hours + 2) - fee", {"rate": 30, "hours": 6, "fee": 15}) -> 225
evaluate("(1 + 2") -> error
Constraints
- Expressions up to 10^5 characters; aim for
O(n)time and space. - Variable names start with a letter and contain letters, digits and
_.
Clarifying questions
- Is
^right-associative, and how does it bind relative to unary minus? - How should division and modulo behave on negative numbers?
- What happens on overflow beyond 64 bits?
- What should a variable without a value do: raise, or leave the expression partly symbolic?
- Can a negative exponent appear, and what should it mean for integers?
What a strong answer covers
- A one-pass tokenizer (numbers, names, operators, parentheses) that rejects unknown characters.
- Either a recursive-descent parser with one function per precedence level, or a shunting-yard algorithm with operand and operator stacks; correct handling of right-associativity and of unary minus.
- Truncating division implemented explicitly in languages where
/floors (Python). - Clear error reporting with a position, and tests for nested parentheses, unary minus before a parenthesis, chains of
^, and very long inputs (recursion depth).
Follow-ups
- Parse once into a tree, then evaluate it many times with different variable maps.
- Return the expression simplified when some variables are unknown (
x + 2 * 3givesx + 6). - Add functions such as
max(a, b)and comparison operators.
Where it's asked
- Snowflake · Two coding screens · Last asked Sep 202560 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
What it tests
parsing · stacks · expressions
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
