Find the Value of a Postfix Expression
Updated
Problem statement
You are given an arithmetic expression in postfix form (also called reverse Polish notation), split into an array of tokens tokens. In postfix form each operator comes right after its two operands, so no brackets are needed: 8 5 - means 8 - 5, and 8 5 - 2 * means (8 - 5) * 2.
Each token is an integer or one of the operators +, -, * and /. An operator works on the two values just before it, and the value that comes first is its left operand. Return the value of the whole expression.
Division is integer division that drops the fractional part, so it rounds toward zero: 7 / 2 = 3 and -7 / 2 = -3.
Function
evalRPN(tokens: String[]) → intExamples
Example 1
tokens = ["7","3","-","4","*"]return = 16Read from left to right. 7 3 - gives 7 - 3 = 4. Then 4 4 * gives 16. As an ordinary expression this is (7 - 3) * 4.
Example 2
tokens = ["-9","2","/","5","+"]return = 1"-9" is one number, minus nine. -9 / 2 is -4.5, and dropping the fractional part gives -4 (not -5). Then -4 + 5 = 1.
Example 3
tokens = ["2","10","3","/","-","6","*"]return = -610 3 / gives 3. Then 2 3 - gives 2 - 3 = -1: the number that came first is on the left. Finally -1 * 6 = -6. As an ordinary expression this is (2 - 10 / 3) * 6.
Constraints
1 <= tokens.length <= 9999- Each
tokens[i]is"+","-","*","/", or an integer from-1000to1000written without leading zeros (a negative number starts with-). tokensis a valid postfix expression: reading from left to right, every operator has at least two values before it, and exactly one value is left at the end.- No division by zero happens, and every intermediate value and the answer are between
-2^31and2^31 - 1.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Evaluate Reverse Polish NotationAtlassian · OAAtlassian interview questions
- Evaluate Reverse Polish NotationCoalition · PHONE SCREEN
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Squirrel Nut Collection Shortest PathAmazon · ONSITE INTERVIEW
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)