Back to Practice
#0263
Evaluate Reverse Polish Notation
MediumDSA28 min20 XP
Problem
Given tokens in Reverse Polish Notation, return the integer result. Division should truncate toward zero.
Why This Matters
RPN removes parentheses by making operation order explicit, which is a clean intro to expression parsers.
Function Signature
def eval_rpn(tokens):
Examples
Example 1
Inputtokens = ["2","1","+","3","*"]
Output9
(2 + 1) * 3 equals 9.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Addition then multiply
Input["2","1","+","3","*"]
Expected9
The + result becomes the left side of multiplication.
Division truncates
Input["4","13","5","/","+"]
Expected6
13 / 5 truncates to 2, then 4 + 2 = 6.
Hidden Test Categories
negative numberssubtraction ordersingle number expression