Evaluate arithmetic expression using DFS
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: HR Screen
Quick Answer: This question evaluates a candidate's understanding of expression parsing and evaluation using depth-first traversal, covering competencies in recursive descent or stack-based DFS, operator precedence, unary operators, and 64-bit integer division semantics with truncation toward zero.
Constraints
- The expression contains only digits, '+', '-', '*', '/', '(', ')', and spaces.
- All integer literals are non-negative; negative values arise only from unary minus.
- The expression is well-formed (balanced parentheses, no division by zero).
- Division truncates toward zero, not toward negative infinity.
- The result fits in a 64-bit signed integer.
Examples
Input: "1 + 2 * 3"
Expected Output: 7
Explanation: Multiplication binds tighter than addition: 2*3=6, then 1+6=7.
Input: "(1 + 2) * 3"
Expected Output: 9
Explanation: Parentheses force the addition first: (1+2)=3, then 3*3=9.
Hints
- Define three mutually recursive parse functions following the grammar: parse_expr handles + and -, parse_term handles * and /, and parse_factor handles unary signs, parentheses, and integer literals. This naturally encodes precedence.
- Maintain a single shared cursor index into the string (e.g. a one-element list in Python or a class field) so all parse functions advance the same position. Skip spaces before reading each token.
- For truncate-toward-zero division, compute abs(a)//abs(b) and negate the quotient when exactly one operand is negative — Python's // floors, which differs for negatives (e.g. -7//2 == -4 but truncation gives -3).
- Recursion depth equals the maximum parenthesis nesting; for adversarial inputs convert to an iterative two-stack algorithm (values + operators) and pop/apply whenever the incoming operator has lower-or-equal precedence.