FastPrepFind the Value of a Postfix Expression
Problems/Find the Value of a Postfix Expression

Find the Value of a Postfix Expression

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A post published on October 21, 2021, by a student in an Expedia campus internship drive puts this stack task in the second technical interview. The candidate coded it and discussed its running time and memory use.

The post gives no input format, limits or examples and does not say how division is rounded. Here, the expression comes as an array of tokens, each an integer from -1000 to 1000 or one of + - * /. There are at most 9,999 tokens. Division drops the fractional part (it rounds toward zero). Every step fits in a 32-bit signed integer.

Our version is about 90% the same as the reported question.

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[]) → int

Examples

Example 1

tokens = ["7","3","-","4","*"]return = 16

Read 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 = -6

10 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 -1000 to 1000 written without leading zeros (a negative number starts with -).
  • tokens is 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^31 and 2^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)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int evalRPN(String[] tokens) {
        // Write your code here.
    }
}
Checking account…