Eval RPN
Python data engineering interview problem. Difficulty: intermediate. Pattern: Stacks. About 14 minutes. Part of the Pro drill bank.
Evaluate a Reverse Polish Notation expression. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Some systems store expressions in postfix form so an operator always follows the values it needs. A stack can evaluate this form from left to right. Evaluate tokens containing integers and +, -, *, /. When dividing, truncate toward zero, not toward negative infinity. Write eval_rpn(tokens: list) -> int. ['2', '1', '+', '3', '*'] first makes 3, then multiplies by 3, giving 9. The starter file already has the function name and harness print lines. Keep them and fill only the function body.
Requirements
- Push numeric tokens.
- For each operator, pop the right operand then the left operand.
- Return the final integer and truncate division toward zero.
Constraints
- The expression is valid and uses the four named operators.
- Tokens are strings and the return type is int.
Examples
Input: eval_rpn(['2', '1', '+', '3', '*']) Output: 9 (2+1)*3 = 9.
Topics: lakebench, python, rpn, eval.
More Python interview questions · All interview problems · Learn data engineering
Eval RPN
Interview-style drill: Evaluate a Reverse Polish Notation expression.
Implement `eval_rpn(tokens: list) -> int` for +, -, *, / (truncate toward zero). Example: `["2", "1", "+", "3", "*"]` → `9`. Keep the harness.