Logo for ammarahmed.ca
Medium
Dec 22, 2025
#stack
#math

150. Evaluate Reverse Polish Notation

You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.

Evaluate the expression. Return an integer that represents the value of the expression.

Note that:

  • The valid operators are '+', '-', '*', and '/'.
    • Each operand may be an integer or another expression.
      • The division between two integers always truncates toward zero.
        • There will not be any division by zero.
          • The input represents a valid arithmetic expression in a reverse polish notation.
            • The answer and all the intermediate calculations can be represented in a 32-bit integer.

              Example 1:

              1Input: tokens = ["2","1","+","3","*"] 2Output: 9 3Explanation: ((2 + 1) * 3) = 9

              Example 2:

              1Input: tokens = ["4","13","5","/","+"] 2Output: 6 3Explanation: (4 + (13 / 5)) = 6

              Example 3:

              1Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] 2Output: 22 3Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 4= ((10 * (6 / (12 * -11))) + 17) + 5 5= ((10 * (6 / -132)) + 17) + 5 6= ((10 * 0) + 17) + 5 7= (0 + 17) + 5 8= 17 + 5 9= 22
              • 1 <= tokens.length <= 104
                • tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].

                  Constraints:

                  Notes

                  Intuition

                  Reverse Polish Notation places operators after their operands. A stack naturally handles this—operands accumulate until an operator consumes them, and intermediate results stay on the stack for future operations.

                  Implementation

                  Iterate through tokens. If it's a number, push to stack. If it's an operator, pop the top two values, apply the operation, and push the result back. The final answer is the single value remaining on the stack.

                  Edge-cases

                  Python's integer division // rounds toward negative infinity, not toward zero. Use int(b / a) for truncation toward zero. Also remember operand order matters for subtraction and division—the second popped value is the left operand (b - a, int(b / a)).

                  • Time: O(n) — single pass through tokens
                    • Space: O(n) — stack holds intermediate values

                      Complexity

                      Solution

                      1from typing import List 2 3class Solution: 4 def evaluate(self, operator: str, a: int, b: int) -> int: 5 if operator == "+": 6 return a + b 7 if operator == "*": 8 return a * b 9 if operator == "-": 10 return b - a 11 if operator == "/": 12 return int(b / a) 13 14 raise ValueError(f"invalid operator: '{operator}'") 15 16 def evalRPN(self, tokens: List[str]) -> int: 17 stack = [] 18 for token in tokens: 19 if token == "+" or token == "-" or token == "*" or token == "/": 20 a = stack.pop() 21 b = stack.pop() 22 stack.append(self.evaluate(token, a, b)) 23 else: 24 stack.append(int(token)) 25 return stack[-1] 26 27 28 29 30if __name__ == "__main__": 31 # Include one-off tests here or debugging logic that can be run by running this file 32 # e.g. print(solution.two_sum([1, 2, 3, 4], 3)) 33 solution = Solution() 34