You are given a string expression written in a tiny Lisp-like language. Return the integer it evaluates to. An expression is one of five things: a whole number (possibly negative), a variable name, or one of three forms in parentheses.
(add e1 e2) is the sum of e1 and e2, and (mult e1 e2) is their product. (let v1 e1 v2 e2 ... vk ek body) binds the names one after another (v1 gets the value of e1, then v2 gets e2, and so on, where later expressions can use earlier names), then returns the value of body. A name may be bound again in the same let; the newest value wins.
A name refers to the binding in the innermost enclosing let that set it. Bindings made inside a let disappear when that let ends. Names start with a lowercase letter, followed by lowercase letters or digits, and are never add, let or mult.
Example 1
Input: expression = "(let a 5 (add a (let a 2 (mult a a))))" Output: 9 Why: the inner let hides a = 5 behind a = 2, so mult gives 4; add sees a = 5 again
2 more examples and the constraints
Example 2
Input: expression = "(let n 3 n (mult n 4) n)" Output: 12 Why: n is set to 3, then rebound to 3 × 4 = 12, and the body is n
Example 3
Input: expression = "(add -3 (mult 2 -4))" Output: -11 Why: 2 × −4 = −8, and −3 + −8 = −11
Constraints
- 1 ≤ expression.length ≤ 2,000
- Tokens are separated by single spaces, with no extra spaces at the ends
- The expression is valid, and every intermediate and final value fits in a 32-bit signed integer
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Split and recurse, copy the scopeO(n²)
- One-pass parser with value stacksO(n)
The idea in one sentence
Side by side
| Approach | Time | Relative work for 2,000 characters, deeply nested | Verdict |
|---|---|---|---|
| Split and recurse, copy the scope | O(n²) | Simple; rereads text | |
| One-pass parser with value stacks | O(n) | Best; reads each token once |
n = length of the expression.
Edge cases to test
- Shadowing. An inner let can rebind a name; after it ends, the outer value is back.
- Rebinding in one let. (let n 3 n (mult n 4) n) is 12: the second value is computed with the first.
- Negative numbers. A token starting with
-is a number, not a name. - Names with digits. v1 and v2 are names; check only the first character for a letter.
- Body is a bare name. In (let z 0 z), the last z is followed by
), so it is the body, not a binding.
Common mistakes
- Letting a let's bindings stay visible after the let ends.
- Treating every token in a let as a name–value pair, so a bare-name body is read as a binding.
- Pushing a new value before evaluating it, so (let x 1 x (add x 1) x) sees the wrong x.
How to talk through it
- 1Clarify. Are tokens always split by single spaces? Can numbers be negative? Can names contain digits?
- 2Describe the grammar. Five kinds of expression; say how to tell a binding from the body inside a let.
- 3Start simple. Split at depth-0 spaces and recurse with a copied scope; say it rereads text.
- 4Improve to one pass. Tokenize once, share a position, and give each name a stack of values to push and pop.
- 5Test out loud. Run a shadowing case, a rebinding case and a negative number.