Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Evaluate Reverse Polish Notation

Stacksmedium

Implement `evalRPN(tokens)` that evaluates an expression in **Reverse Polish Notation**. Tokens are strings: either numbers or one of `+`, `-`, `*`, `/`. Division truncates toward zero. Example: `evalRPN(["2","1","+","3","*"])` → `9`.

Sample tests

Input: evalRPN(["2","1","+","3","*"])
Output: 9
Input: evalRPN(["4","13","5","/","+"])
Output: 6

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Operand order matters for subtraction and division.
  • Division must truncate toward zero, not floor.

Learning resources

  • MDN: Array.prototype.push / pop
Approach & explanation (try first)

A stack holds operands; each operator pops two and pushes the result. O(n) time and space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.