Evaluate Reverse Polish Notation
Try to solve the Evaluate Reverse Polish Notation challenge.
We'll cover the following
Statement#
Given an arithmetic expression in a Reverse Polish Notation (RPN) as an array of strings, tokens, your task is to evaluate and return the value of the given expression.
Points to consider:
-
Valid operators are
+,-,*, and/. -
Each operand can be an integer or another expression.
-
The division between two integers should truncate toward zero.
The given Reverse Polish Notation expression is guaranteed to be valid. This ensures that the expression always evaluates to a result and that there are no division-by-zero operations.
Constraints:
-
tokens.length -
tokens[i]is either an operator (+,-,*, or/) or an integer in the range .
Examples#
1 of 3
2 of 3
3 of 3
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Reverse Polish Notation
What does the following arithmetic expression evaluate to?
tokens = [ “”, “”, “”, “”, “” ]
5
-5
19
-19
Try it yourself#
Implement your solution in the following coding playground.
The optimal solution to this problem runs in O(n) time and takes O(n) space.
You might want to go over the Stacks pattern again.
Solution: Basic Calculator
Solution: Evaluate Reverse Polish Notation