150. Evaluate Reverse Polish Notation
mediumAsked at LinkedInGiven an array of tokens in Reverse Polish (postfix) notation, evaluate and return the result. LinkedIn asks this because it's the textbook stack problem — they want clean push-on-operand, pop-pop-apply-push-on-operator without any parsing distractions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in LinkedIn loops.
- Glassdoor (2026-Q1)— LinkedIn SWE onsite reports consistently include this as the stack-based algorithm question.
- Blind (2025-11)— LinkedIn writeups list this among their highest-frequency mid-level questions in 2026-Q1.
Problem
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation. Evaluate the expression. Return an integer that represents the value of the expression. Note: The valid operators are '+', '-', '*', and '/'. Each operand may be an integer or another expression. The division between two integers always truncates toward zero. There will not be any division by zero. The input represents a valid arithmetic expression.
Constraints
1 <= tokens.length <= 10^4tokens[i] is either an operator: '+', '-', '*', or '/', or an integer in the range [-200, 200].
Examples
Example 1
tokens = ["2","1","+","3","*"]9Explanation: ((2 + 1) * 3) = 9.
Example 2
tokens = ["4","13","5","/","+"]6Explanation: (4 + (13 / 5)) = 6.
Approaches
1. Stack-based evaluation (canonical)
Push operands. On an operator, pop the top two, apply the operator (b op a where a was popped first), push the result.
- 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 b = stack.pop();
const a = stack.pop();
stack.push(ops[t](a, b));
} else {
stack.push(parseInt(t, 10));
}
}
return stack[0];
}Tradeoff: O(n) and one pass. The order matters — pop b first then a, then evaluate a op b. Reversing gives wrong subtraction and division.
2. Recursive postorder evaluation
Walk tokens from the right; each operator recursively consumes its two operands.
- Time
- O(n)
- Space
- O(n) recursion
function evalRPNRec(tokens) {
let i = tokens.length - 1;
function take() {
const t = tokens[i--];
if (t === '+' || t === '-' || t === '*' || t === '/') {
const b = take();
const a = take();
if (t === '+') return a + b;
if (t === '-') return a - b;
if (t === '*') return a * b;
return Math.trunc(a / b);
}
return parseInt(t, 10);
}
return take();
}Tradeoff: Same asymptotic complexity but trickier to write correctly (the right-to-left pointer is easy to break). Stack version is the interview default.
LinkedIn-specific tips
LinkedIn interviewers specifically watch the operand-order on subtraction and division. State out loud: 'I pop b first, then a, then compute a OP b — because the left operand was pushed earlier.' Many candidates get the addition/multiplication right (commutative) but flip subtraction and don't notice. Also use Math.trunc for division, NOT Math.floor — they differ for negative results (-1 truncated is 0, floored is -1).
Common mistakes
- Popping in the wrong order — gives correct answers for + and * but wrong for - and /.
- Using Math.floor instead of Math.trunc for division — gives wrong answers for negative operands.
- Forgetting to parseInt the operand strings — leaves you with string concatenation on '+'.
Follow-up questions
An interviewer at LinkedIn may pivot to one of these next:
- Basic Calculator (LC 224) — infix expression with parens.
- Basic Calculator II (LC 227) — infix with precedence.
- Convert infix to RPN, then evaluate — Shunting-yard algorithm follow-up.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is RPN easier to evaluate than infix?
Because operator precedence and parens are encoded in the token order — you never need a lookahead. The stack does all the bookkeeping naturally.
What's special about LinkedIn asking this?
LinkedIn likes algorithmic problems that compose well with their other questions (Basic Calculator, expression parsing). RPN is the cleanest starting point — once you can evaluate RPN, infix is just 'convert to RPN, then evaluate' (Shunting-yard).
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Evaluate Reverse Polish Notation and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →