Evaluate a Prefix Expression
Updated
Problem statement
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 bmeansa - b. - Division truncates toward zero, so
-7 / 2is-3. - A token such as
"-4"is a negative number. Only the single-character token"-"is the subtraction operator.
Function
evaluatePrefix(tokens: String[]) → intExamples
Example 1
tokens = ["+","3","*","4","5"]return = 23The + 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 = 10The 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 = -2The 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^4to10^4written without leading zeros or a plus sign. tokensis 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