Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

86. Evaluate Reverse Polish Notation

MediumDay 25 of 60Stack & Queue

You are given an arithmetic expression in Reverse Polish Notation (postfix) as an array of string tokens. Each token is an integer or one of the operators +, -, * and /. Evaluate the expression and return the result as an integer. Division truncates toward zero, there is never a division by zero, and the expression is always valid.

Example 1
Input: tokens = ["2","1","+","3","*"]
Output: 9
(2 + 1) * 3 = 9
Example 2
Input: tokens = ["4","13","5","/","+"]
Output: 6
4 + (13 / 5) = 4 + 2 = 6
Constraints
  • 1 <= tokens.length <= 10^4
  • Each token is an operator or an integer in [-200, 200]
  • Every intermediate result fits in a 32-bit integer
Hints
Frequently asked at: Amazon, LinkedIn, Google
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min