By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can handle stack-based evaluation, edge cases, and clean code under pressure."
Before tackling RPN, ensure you understand: 1. Stacks (LIFO) – Push/pop operations, time complexity (O(1) for both). 2. Basic arithmetic operations – How to evaluate +, -, , / with correct order of operations. 3. String parsing – Splitting tokens, converting strings to integers.
O(1)
+
-
/
["2","1","+"]
3
2,1
2+1=3
"3 4 +"
["3","4","+"]
["1","0","/"]
ZeroDivisionError
["+"]
Invalid RPN
6 / -132
0
-1
Run this every time you see an RPN problem:
second = stack.pop()
first = stack.pop()
first OP second
Problem: Evaluate ["2","1","+","3",""] → (2+1)3 = 9
["2","1","+","3",""]
(2+1)3 = 9
[2]
[2, 1]
[3]
[3, 3]
33=9
[9]
Final Answer: 9
9
def evalRPN(tokens): stack = [] for token in tokens: if token in "+-/": b = stack.pop() a = stack.pop() if token == "+": stack.append(a + b) elif token == "-": stack.append(a - b) elif token == "": stack.append(a b) elif token == "/": stack.append(int(a / b)) # Truncate toward zero else: stack.append(int(token)) return stack.pop()
Time Complexity: O(n) (one pass through tokens) Space Complexity: O(n) (stack size ≤ n/2)
O(n)
n/2
Why this works: - Stack ensures correct order of operations (postfix evaluation). - Division truncates toward zero (Python’s int(a / b) behavior).
int(a / b)
Problem: Evaluate ["4","13","5","/","+"] → 4 + (13/5) = 6
["4","13","5","/","+"]
4 + (13/5) = 6
[4]
[4, 13]
[4, 13, 5]
13/5=2
[4, 2]
4+2=6
[6]
Final Answer: 6
6
def evalRPN(tokens): stack = [] for token in tokens: if token in "+-/": b = stack.pop() a = stack.pop() if token == "+": stack.append(a + b) elif token == "-": stack.append(a - b) elif token == "": stack.append(a b) elif token == "/": stack.append(int(a / b)) else: stack.append(int(token)) return stack.pop()
Edge Case: ["10","6","9","3","+","-11","","/","","17","+","5","+"] → 22 (Try this yourself!)
["10","6","9","3","+","-11","","/","","17","+","5","+"]
22
Why this works: - Handles negative numbers and division correctly. - Stack ensures operands are popped in reverse order (critical for - and /).
Problem: Extend RPN to support exponentiation (^) and modulo (%).
^
%
def evalRPN(tokens): stack = [] for token in tokens: if token in "+-/%^": b = stack.pop() a = stack.pop() if token == "+": stack.append(a + b) elif token == "-": stack.append(a - b) elif token == "": stack.append(a b) elif token == "/": stack.append(int(a / b)) elif token == "%": stack.append(a % b) elif token == "^": stack.append(a b) else: stack.append(int(token)) return stack.pop()
Test Case: ["2","3","^","4","%"] → (2^3) % 4 = 0
["2","3","^","4","%"]
(2^3) % 4 = 0
Why this works: - Extensible design: New operators require only one additional elif. - Same stack logic applies—no changes to the core algorithm.
elif
second
first
//
6 / -132 = 0
if b == 0 and token == "/": return error
eval()
if len(stack) != 1: return error
1 / 0
-1 / 2
"Alright, let’s lock this in. Reverse Polish Notation is all about stacks and order of operations. Here’s the playbook:
You’ve got this. Walk in, write the stack, handle the edge cases, and you’ll crush it. Good luck!
Now go ace that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.