Evaluate Reverse Polish Notation — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Evaluate an arithmetic expression in Reverse Polish Notation.” This is a classic stack problem — operators consume the two operands on top of the stack. Recognizing the stack pattern makes the implementation trivially short, but the details — operand order and Python division truncation — are what separate clean answers from buggy ones.
The Problem
Evaluate an expression in Reverse Polish Notation (postfix notation). Valid operators are +, -, *, /. Integer division should truncate toward zero.
Example
Input: tokens = ["2","1","+","3","*"]
Output: 9 — ((2 + 1) * 3) = 9
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Stack recognition | Do you immediately identify this as a stack problem? |
| Operand order | Do you pop operands in the correct order? |
| Integer division truncation | Do you truncate toward zero, not floor? |
| Clean code | Is the operator dispatch readable? |
Step 1: The Key Insight
RPN is designed for stack-based evaluation. Push integers onto the stack. When you encounter an operator, pop two operands, apply the operator, and push the result. After processing all tokens, one number remains — that’s the answer.
---
Step 2: Operand Order
When popping for - and /, order matters:
b = stack.pop()(right operand)a = stack.pop()(left operand)- Result:
a op b
Python Implementation
def evalRPN(tokens: list[str]) -> int:
stack = []
operators = {'+', '-', '*', '/'}
for token in tokens:
if token in operators:
b = stack.pop()
a = stack.pop()
if token == '+':
stack.append(a + b)
elif token == '-':
stack.append(a - b)
elif token == '*':
stack.append(a * b)
elif token == '/':
stack.append(int(a / b)) # truncate toward zero
else:
stack.append(int(token))
return stack[0]
Division note: int(a / b) truncates toward zero. Python’s // floors toward negative infinity, which differs for negative results: int(7 / -2) = -3 (correct), but 7 // -2 = -4 (wrong).
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(n) — single pass through tokens |
| Space | O(n) — stack holds at most n elements |
Common Interview Mistakes
-
Wrong operand order. Pop
bfirst (right), thena(left).a - b, notb - a. -
Using
//instead ofint(a / b). Python’s//floors; the problem wants truncation toward zero. -
Not converting tokens to integers. Tokens are strings — push
int(token).
What a Strong Candidate Sounds Like
“This is a stack problem. I push numbers as I encounter them. When I see an operator, I pop two — the first pop is the right operand, the second is the left — apply the operator, and push the result. One subtlety: Python’s
//floors toward negative infinity, but the problem wants truncation toward zero, so I useint(a / b)instead.”
Example Interview Dialogue
Interviewer: Why does division direction matter in Python?
Candidate: For positive numbers, // and truncation are identical. For negative results they differ: 7 // -2 = -4 (floor), but the problem expects -3 (truncate toward zero). int(7 / -2) = int(-3.5) = -3.
Related Problems to Practice
- Min Stack — Stack design with auxiliary state.
- Valid Parentheses — Classic stack matching.
- Basic Calculator — More complex expression evaluation with parentheses.
Practice This in a Mock Interview
Evaluate RPN is fast if you recognize the stack pattern immediately. The main pitfall is division truncation in Python — know the int(a/b) vs. a//b distinction before your interview.
Start a mock interview for Evaluate Reverse Polish Notation on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub