Interview conceptSoftware Engineering Fundamentals

Parsing, Expression Evaluation, And Interpreters

Asked of: Software Engineer

Last updated

What's being tested

These problems test recursive descent parsing, operator-precedence evaluation, and stack-machine interpretation over structured input. You must translate tokens or instructions into well-defined execution steps while preserving operand order, associativity, runtime types, and exact arithmetic. Strong solutions separate parsing, representation, and evaluation so each layer is testable and extensible.

Patterns & templates

  • Recursive descent: use functions such as parse_expression, parse_term, and parse_factor; encode precedence directly in the call graph.

  • Shunting-yard algorithm: maintain an operator stack and output queue; process precedence and associativity in O(n) time and space.

  • Stack-machine execution: pop the right operand first, then the left; compute left op right, especially for subtraction and division.

  • Typed runtime values: represent integers and strings distinctly, using tagged unions or classes; define mixed-type operations explicitly rather than coercing accidentally.

  • Exact arithmetic: use integer arithmetic or reduced rational pairs (numerator, denominator); avoid floating-point equality and rounding errors.

  • Backtracking search: for “use each number once,” recurse over unused indices and operator choices; memoize canonical states when repeated subproblems exist.

  • AST representation: parse into an abstract syntax tree before evaluation when validation, debugging, multiple passes, or future operators matter; construction costs O(n).

Common pitfalls

Pitfall: Applying operators immediately from left to right ignores precedence and associativity; distinguish 2 + 3 * 4 from (2 + 3) * 4.

Pitfall: Popping operands in the wrong order silently breaks noncommutative operators; document stack invariants and test subtraction, division, and malformed input.

Pitfall: Treating strings and integers uniformly causes invalid concatenation, comparison, or output behavior; define runtime type rules before implementing ADD.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Practice questions

Related concepts