ExamAdda Logo

Evaluate Reverse Polish Notation

Easy

Description

Given an array of strings tokens that represents an arithmetic expression in Reverse Polish Notation, evaluate the expression and return the integer result.

Valid operators are:

+ - * /

 

Each operand may be an integer or another expression.

Division between two integers should truncate toward zero.

Example 1

Input

5
2 1 + 3 *

Output

9

Explanation

(2 + 1) * 3 = 9

Example 2

Input

5
4 13 5 / +

Output

6

Explanation

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

Constraints

1 ≤ tokens.length ≤ 10⁴
tokens[i] is either an operator or an integer.
The expression is always valid.
Division truncates toward zero.

Hints:

Hint 1

Use a stack to store operands.

Hint 2

When you encounter an operator:

  1. Pop the second operand.
  2. Pop the first operand.
  3. Apply the operator.
  4. Push the result back onto the stack.

The final value remaining in the stack is the answer.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 9, 2026

Last updated

Aug 9, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output