FastPrepEvaluate a Prefix Expression
Problems/Evaluate a Prefix Expression

Evaluate a Prefix Expression

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2026 report on a Google FDE coding round names only the kind of problem: evaluating an expression written in prefix notation.

The report gives no input format, operator set, or division rule. Here, the expression is an array of tokens with +, -, *, and / on integers. The first operand comes first, division truncates toward zero, negative numbers can appear as tokens, and all values fit in 32 bits. The limits are our choice.

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

In prefix notation every operator is written before its two operands, so no parentheses are needed. For example, * + 2 3 4 means (2 + 3) * 4. Each operand is either a number or another prefix expression.

You are given the expression as an array tokens, read from left to right. Return its value.

  • The operators are +, -, * and /. For - and /, the first operand is on the left: - a b means a - b.
  • Division truncates toward zero, so -7 / 2 is -3.
  • A token such as "-4" is a negative number. Only the single-character token "-" is the subtraction operator.

Function

evaluatePrefix(tokens: String[]) → int

Examples

Example 1

tokens = ["+","3","*","4","5"]return = 23

The + applies to 3 and to the expression * 4 5, which is 20. The value is 3 + 20 = 23.

Example 2

tokens = ["-","/","20","3","-4"]return = 10

The first operand of - is / 20 3, which is 6 after truncation. The second operand is the number -4. The value is 6 - (-4) = 10.

Example 3

tokens = ["/","-","1","8","3"]return = -2

The inner expression - 1 8 is -7. Then -7 / 3 truncates toward zero to -2, not -3.

Constraints

  • 1 <= tokens.length <= 1001
  • Each token is "+", "-", "*", "/", or a whole number from -10^4 to 10^4 written without leading zeros or a plus sign.
  • tokens is one complete, valid prefix expression.
  • No division by zero happens, and every intermediate result and the final result fit in a signed 32-bit integer.

More Google Onsite Interview questions

  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
  • Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
  • Smallest Value for a Linear Expression ModuloIntel · OA
  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
  • Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
  • Add Two Numbers IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Lisp Expression ParserAmazon · ONSITE INTERVIEW

Interview reports that mention this question

  • Google FDE and Cloud AI, Experienced: 6 Experiences (Part 4 of 4)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int evaluatePrefix(String[] tokens) {
        // Write your code here.
    }
}
Checking account…