Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Evaluate Reverse Polish Notation

Medium
stackarraymath

Evaluate an arithmetic expression given in Reverse Polish (postfix) notation. Valid operators are +, -, *, /. Division truncates toward zero. The expression is always valid and no division by zero occurs.

Input: one line of space-separated tokens. Output: the value of the expression.

Example 1
Input:
2 1 + 3 *
Output:
9

(2 + 1) × 3.

Example 2
Input:
4 13 5 / +
Output:
6

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

Example 3
Input:
10 6 9 3 + -11 * / * 17 + 5 +
Output:
22

Constraints

  • 1 ≤ tokens.length ≤ 10^4
  • Intermediate values fit in a 32-bit integer.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.