150. Evaluate Reverse Polish Notation - Detailed Explanation
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Problem Statement
You’re given an array of strings tokens
representing an arithmetic expression in Reverse Polish Notation. Evaluate the expression and return the result as an integer. Valid operators are +
, -
, *
, and /
. Each operand may be an integer or another expression in RPN. Division between two integers should truncate toward zero.
Examples
Example 1
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation:
Step by step:
9 3 + → 12
12 –11 * → –132
6 ÷ (–132) → 0 (truncated)
10 * 0 → 0
0 + 17 → 17
17 + 5 → 22
Constraints
- 1 ≤ tokens.length ≤ 10⁴
- Each
tokens[i]
is either an operator"+"
,"-"
,"*"
,"/"
or an integer in the range [−200, 200]. - The given RPN expression is always valid and the result fits in a 32‑bit signed integer.
Hints
- How would you evaluate an expression in infix notation? Can you adapt that to RPN?
- When you see a number, you need to store it until you have two operands for an operator.
- A stack is perfect: push numbers, and when you see an operator, pop two, compute, then push the result.
Approach (Stack‑Based One‑Pass)
- Initialize an empty stack of integers.
- Iterate through each token in
tokens
:- If it’s a number (check by trying to parse), convert and push onto the stack.
- Otherwise it’s an operator:
- Pop the top two values in LIFO order: let
b = stack.pop()
, thena = stack.pop()
. - Compute
res = apply(a, b, operator)
. - Push
res
back onto the stack.
- Pop the top two values in LIFO order: let
- After processing all tokens, the stack contains exactly one value, the final result. Pop and return it.
Why It Works
Reverse Polish Notation is designed so that you never need to look backward or worry about precedence or parentheses. By pushing operands and applying operators immediately when they appear, you naturally respect the intended grouping.
Step‑by‑Step Walkthrough
Take ["2","1","+","3","*"]
:
- See
"2"
→ push 2; stack = [2] - See
"1"
→ push 1; stack = [2,1] - See
"+"
→ pop b=1, a=2 → compute 2+1=3 → push 3; stack = [3] - See
"3"
→ push 3; stack = [3,3] - See
"*"
→ pop b=3, a=3 → compute 3*3=9 → push 9; stack = [9]
End → pop 9 as result.
Complexity Analysis
- Time: O(n), where n = number of tokens, since each token is pushed or popped at most once.
- Space: O(n), for the stack in the worst case (all numbers).
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Operand order: For subtraction and division, order matters. Always pop b then a and compute
a op b
. - Integer division: In many languages, default division may floor negatives. Ensure truncation toward zero. In Python 3,
int(a/b)
or usemath.trunc
; in Java, integer/
already truncates toward zero. - Stack underflow: The problem guarantees validity, but defensively ensure the stack has at least two elements before popping.
Edge Cases
- Single number token → just return that number.
- Division by a negative number and truncation.
- Large intermediate values may overflow 32‑bit if not careful, but here constraints ensure final fits.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.