📖 3 min read
Last updated on

Evaluate Reverse Polish Notation — Coding Interview Walkthrough


Hero Image for 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

SkillWhat Interviewers Observe
Stack recognitionDo you immediately identify this as a stack problem?
Operand orderDo you pop operands in the correct order?
Integer division truncationDo you truncate toward zero, not floor?
Clean codeIs 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.

Vertical stack showing RPN evaluation of [2,1,+,3,*]: intermediate values 2 and 3 below, result 9 at top with teal highlight — no linked-list arrows. ---

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

AspectComplexity
TimeO(n) — single pass through tokens
SpaceO(n) — stack holds at most n elements

Common Interview Mistakes

  1. Wrong operand order. Pop b first (right), then a (left). a - b, not b - a.

  2. Using // instead of int(a / b). Python’s // floors; the problem wants truncation toward zero.

  3. 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 use int(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.



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:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →