Given an array of strings representing an arithmetic expression in Reverse Polish Notation (postfix notation), explain how you would evaluate it.
In RPN, operators come after their operands. For example:
- ["2", "3", "+"] evaluates to 5 (2 + 3)
- ["4", "13", "5", "/", "+"] evaluates to 6 (4 + (13 / 5) = 4 + 2 = 6)
- ["2", "1", "+", "3", "*"] evaluates to 9 ((2 + 1) * 3)
Valid operators are +, -, *, and /. Division truncates toward zero.
Explain your approach and analyze the time and space complexity.
How to approach it
- Hint 1
Think about which data structure lets you naturally track operands and apply operators as you encounter them.
- Hint 2
Use a stack: push numbers, and when you hit an operator, pop two operands, apply the operator, and push the result.
- Hint 3
Be careful about operand order for subtraction and division: the first popped value is the right operand, the second popped value is the left operand.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.