16 · Stacks & Monotonic Stack

#150 Evaluate Reverse Polish Notation

Mediumsolve on LeetCode ↗

frequently reported atLinkedInAmazonGoogleMeta

the problem

In plain words

Evaluate an arithmetic expression written in Reverse Polish (postfix) notation, given as an array of tokens. Operators are + − * /. Division between integers truncates toward zero. The expression is always valid.

in tokens = ["2","1","+","3","*"]

out 9

((2 + 1) * 3) = 9

in tokens = ["4","13","5","/","+"]

out 6

4 + (13 / 5) = 4 + 2 = 6

in tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]

out 22

9 + 3 = 12; 12 × −11 = −132; 6 / −132 = 0 (truncated); 10 × 0 = 0; 0 + 17 + 5 = 22.

  • 1 ≤ tokens.length ≤ 10⁴
  • Each token is an operator or an integer in [−200, 200]
  • No division by zero; every intermediate value fits in a 32-bit int

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #394
      Decode String
      Mediumin Stacks
    2. #20
      Valid Parentheses
      Easyin Stacks