Evaluate a Lisp-like expression with let bindings, add, mult and nested variable scopes

Quick Overview

Write an evaluator for a small Lisp-like language with integers, variables, add, mult and let expressions, and return the integer value of one expression. It tests recursive descent parsing and how to handle sequential let bindings and nested variable scopes.

Evaluate a Lisp-like expression with let bindings, add, mult and nested variable scopes

Company: Affirm

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a string `expression` that holds one expression in a small Lisp-like language. Return the integer it evaluates to. The syntax is: - An expression is an integer, a variable, a `let` expression, an `add` expression or a `mult` expression. Every expression evaluates to a single integer. - An integer can be positive or negative, such as `7` or `-12`. - `(add e1 e2)` evaluates `e1` and `e2` and returns their sum. - `(mult e1 e2)` evaluates `e1` and `e2` and returns their product. - `(let v1 e1 v2 e2 ... vn en expr)` has one or more pairs of a variable and an expression, followed by one final expression. The pairs are processed in order: `v1` is assigned the value of `e1`, then `v2` is assigned the value of `e2`, and so on. The value of the whole `let` is the value of `expr`. - A variable starts with a lowercase letter, followed by zero or more lowercase letters or digits. The names `add`, `let` and `mult` are reserved and are never used as variable names. Variables are scoped by parentheses. When a variable is evaluated, the innermost scope is checked first for its value, then the outer scopes in order, moving outward. The expression is always legal. ### Function Signature ```python def evaluate(expression: str) -> int: ``` ### Rules - Parentheses sit directly against the token next to them, as in `(add x 1)`. - Inside one `let`, each `ei` is evaluated before `vi` is assigned. So `ei` sees the earlier assignments of the same `let`, and a later assignment to a name replaces the earlier one for the rest of that `let`. - The assignments a `let` makes are visible only inside that `let`. After its closing parenthesis, each variable it assigned has the value from the enclosing scope again. - Every variable that gets evaluated has a value at that point. ### Constraints - `1 <= len(expression) <= 2000` - `expression` has no leading or trailing spaces. - All tokens in `expression` are separated by a single space. - The answer and every intermediate result fit in a 32-bit signed integer. - The expression is legal and can be evaluated. ### Examples ```text Input: expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))" Output: 14 ``` The outer `let` sets `x = 2`. The inner `let` sets `x = 3` and `y = 4`, so `(add x y)` is 7. The first operand of `mult` is outside the inner `let`, so it reads the outer `x = 2`, and the result is 2 * 7 = 14. ```text Input: expression = "(let x 3 x 2 x)" Output: 2 ``` The assignments run in order: `x` becomes 3 and is then reassigned to 2, so the final `x` evaluates to 2. ```text Input: expression = "(let x -2 y (add x 5) (mult x y))" Output: -6 ``` `y` is assigned after `x`, so `(add x 5)` sees `x = -2` and gives 3. The final expression is `(mult x y)`, which is -2 * 3 = -6.

Overview: Write an evaluator for a small Lisp-like language with integers, variables, add, mult and let expressions, and return the integer value of one expression. It tests recursive descent parsing and how to handle sequential let bindings and nested variable scopes.

Affirm logo
Affirm
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string expression that holds one expression in a small Lisp-like language. Return the integer it evaluates to.

The syntax is:

  • An expression is an integer, a variable, a let expression, an add expression or a mult expression. Every expression evaluates to a single integer.
  • An integer can be positive or negative, such as 7 or -12 .
  • (add e1 e2) evaluates e1 and e2 and returns their sum.
  • (mult e1 e2) evaluates e1 and e2 and returns their product.
  • (let v1 e1 v2 e2 ... vn en expr) has one or more pairs of a variable and an expression, followed by one final expression. The pairs are processed in order: v1 is assigned the value of e1 , then v2 is assigned the value of e2 , and so on. The value of the whole let is the value of expr .
  • A variable starts with a lowercase letter, followed by zero or more lowercase letters or digits. The names add , let and mult are reserved and are never used as variable names.

Variables are scoped by parentheses. When a variable is evaluated, the innermost scope is checked first for its value, then the outer scopes in order, moving outward. The expression is always legal.

Function Signature

def evaluate(expression: str) -> int:

Rules

  • Parentheses sit directly against the token next to them, as in (add x 1) .
  • Inside one let , each ei is evaluated before vi is assigned. So ei sees the earlier assignments of the same let , and a later assignment to a name replaces the earlier one for the rest of that let .
  • The assignments a let makes are visible only inside that let . After its closing parenthesis, each variable it assigned has the value from the enclosing scope again.
  • Every variable that gets evaluated has a value at that point.

Constraints

  • 1 <= len(expression) <= 2000
  • expression has no leading or trailing spaces.
  • All tokens in expression are separated by a single space.
  • The answer and every intermediate result fit in a 32-bit signed integer.
  • The expression is legal and can be evaluated.

Examples

Input: expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))"
Output: 14

The outer let sets x = 2. The inner let sets x = 3 and y = 4, so (add x y) is 7. The first operand of mult is outside the inner let, so it reads the outer x = 2, and the result is 2 * 7 = 14.

Input: expression = "(let x 3 x 2 x)"
Output: 2

The assignments run in order: x becomes 3 and is then reassigned to 2, so the final x evaluates to 2.

Input: expression = "(let x -2 y (add x 5) (mult x y))"
Output: -6

y is assigned after x, so (add x 5) sees x = -2 and gives 3. The final expression is (mult x y), which is -2 * 3 = -6.