150. Evaluate Reverse Polish Notation
mediumAsked at PinterestEvaluate Reverse Polish Notation is a Pinterest stack warm-up: given tokens in postfix notation, evaluate the expression. The interviewer is checking that you reach for a stack and handle integer division truncation correctly.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Pinterest loops.
- Glassdoor (2026-Q1)— Pinterest SWE onsite reports cite RPN as the stack round.
- LeetCode Pinterest tag (2026-Q1)— On the Pinterest company-tagged problem list.
Problem
You are given an array of strings tokens that represents an arithmetic expression in Reverse Polish Notation. Evaluate the expression. Return an integer that represents the value of the expression. Note that division between two integers should truncate toward zero.
Constraints
1 <= tokens.length <= 10^4tokens[i] is either '+', '-', '*', '/', or an integer in the range [-200, 200].The answer and all intermediate results fit in a 32-bit integer.Division by zero is not allowed.
Examples
Example 1
tokens = ['2','1','+','3','*']9Explanation: ((2 + 1) * 3) = 9.
Example 2
tokens = ['4','13','5','/','+']6Explanation: (4 + (13 / 5)) = 6 (integer division truncates 2.6 to 2).
Approaches
1. Stack (only viable approach)
Push numbers onto a stack. On an operator, pop two operands (right first, then left), apply, push the result. Final stack top is the answer.
- Time
- O(n)
- Space
- O(n)
function evalRPN(tokens) {
const stack = [];
const ops = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => Math.trunc(a / b)
};
for (const t of tokens) {
if (t in ops) {
const right = stack.pop();
const left = stack.pop();
stack.push(ops[t](left, right));
} else {
stack.push(parseInt(t, 10));
}
}
return stack[0];
}Tradeoff: There's no meaningful brute force — postfix evaluation IS a stack problem. The interesting tradeoffs are: (1) Math.trunc vs Math.floor for division (trunc rounds toward zero, the problem requires this); (2) operand order matters for subtraction and division.
Pinterest-specific tips
Pinterest interviewers care about three details: (1) operand order for non-commutative ops — left = pop2, right = pop1 is wrong; right = pop2, left = pop1 is right; (2) integer division truncates toward zero — Math.trunc(-7/2) = -3 vs Math.floor(-7/2) = -4; the problem requires trunc; (3) parseInt with explicit radix 10. Walk through these out loud.
Common mistakes
- Reversing the operand order — '4 13 / +' should compute 13 / 4? No: when '/' fires, right = top of stack = 13, left = second from top = 4 — that's wrong. Re-read the example. Actually pop order: first pop = right operand, second pop = left operand.
- Using Math.floor instead of Math.trunc for division — fails on negative dividends.
- Using parseInt without radix — '0X' tokens parse incorrectly (LeetCode doesn't produce these but it's good practice).
- Not handling the single-number input — tokens = ['42'] should return 42.
Follow-up questions
An interviewer at Pinterest may pivot to one of these next:
- Convert infix to postfix (Shunting-yard algorithm).
- Basic Calculator (LeetCode 224 / 227) — infix expression with parentheses and precedence.
- Support floating-point operands.
- Generate all RPN expressions that evaluate to a target value.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is RPN easier than infix arithmetic?
Postfix has no precedence ambiguity and no parentheses. The stack discipline handles it in one pass; infix requires shunting-yard or recursive-descent parsing.
Why does Pinterest care about expression evaluation?
It's a clean signal on stack fluency and operator-order discipline. The follow-ups (basic calculator) test more advanced parsing skills.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Evaluate Reverse Polish Notation and other Pinterest interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →