Reverse Polish Notation eliminates parentheses by placing operators after their operands. A single operand stack evaluates any RPN expression in one linear scan — numbers go on, operators pull two off and push the result.
Given a list of tokens representing an expression in Reverse Polish Notation, return its integer result. Operators are +, -, *, / (truncates toward zero). For example, ["2", "1", "+", "3", "*"] encodes (2 + 1) * 3 and returns 9. Similarly, ["4", "13", "5", "/", "+"] encodes 4 + (13 / 5) and returns 6 (since 13/5 = 2 after truncation).
b (top) then a (next), compute a op b, and push the result. One pass, one stack — no parentheses or precedence rules needed.number[] stack. It will hold the operands waiting to be consumed.parseInt and push onto the stack. Negative integers are valid tokens (e.g. "-3").b = stack.pop() first (it was pushed second), then a = stack.pop(). Compute a op b and push the result. Order matters for - and /.stack[0].a and which is b? The first pop gives b (the right operand — pushed later), and the second pop gives a (the left operand — pushed earlier). Getting this backwards produces the wrong sign on subtraction and a wrong quotient on division. The rule: pop b, pop a, compute a op b.The stack grows at most to half the token count (all numbers, no operators yet). Worst case is O(n) stack entries. There is no constant-space alternative for a general RPN evaluator — you must hold intermediate results somewhere.
1▶function evalRPN(tokens: string[]): number {2▶ const stack: number[] = [];34 for (const tok of tokens) {5 if (tok === '+' || tok === '-' || tok === '*' || tok === '/') {6 const b = stack.pop()!; // second operand (pushed last)7 const a = stack.pop()!; // first operand (pushed first)8 if (tok === '+') stack.push(a + b);9 else if (tok === '-') stack.push(a - b);10 else if (tok === '*') stack.push(a * b);11 else stack.push(Math.trunc(a / b)); // truncate toward zero12 } else {13 stack.push(parseInt(tok, 10));14 }15 }1617 return stack[0];18}
function evalRPN(tokens: string[]): number {
const stack: number[] = [];
for (const tok of tokens) {
if (tok === '+' || tok === '-' || tok === '*' || tok === '/') {
const b = stack.pop()!; // second operand (pushed last)
const a = stack.pop()!; // first operand (pushed first)
if (tok === '+') stack.push(a + b);
else if (tok === '-') stack.push(a - b);
else if (tok === '*') stack.push(a * b);
else stack.push(Math.trunc(a / b)); // truncate toward zero
} else {
stack.push(parseInt(tok, 10));
}
}
return stack[0];
}number[] holds every intermediate operand. Operators consume from it; numbers feed it.if on line 5 checks whether the token is one of the four operators. All other tokens are treated as integer strings.b first (top of stack = right operand), then a (next = left operand). The non-null assertion ! is safe because a valid RPN always has two values available when an operator appears. For division, Math.trunc truncates toward zero as the problem requires (matches C++ integer division)."-3" correctly because parseInt accepts a leading minus sign.stack[0]. No length check needed given the problem guarantees.number[] (already handles floats in JS/TS), drop Math.trunc, and parse with parseFloat.^"? Replace the chain of if/else with a Map<string, (a: number, b: number) => number>; adding an operator is one line.| postfix / RPN expression to evaluate | stack of operands |
| "pop two, apply operator, push result" | pop b then a → a op b |
| division must truncate toward zero | Math.trunc(a / b) |
| nested/matching structure resolved by most-recent | stack (LIFO) |
function evalRPN(tokens: string[]): number {
const stack: number[] = [];
for (const tok of tokens) {
if (isOperator(tok)) {
const b = stack.pop()!; // second operand
const a = stack.pop()!; // first operand
stack.push(apply(tok, a, b));
} else {
stack.push(parseInt(tok, 10));
}
}
return stack[0];
}["2", "1", "+", "3", "*"]. What does the stack look like after processing the + operator?["4", "13", "5", "/", "+"] evaluate to?["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]. What is the result?