Evaluate a Nested Math Expression
Updated
Problem statement
You are given a string s that holds a math expression written as nested function calls. Return the integer value of the expression.
The expression can use five functions. Each one takes exactly two arguments:
add(x, y)returnsx + y.sub(x, y)returnsx - y.mul(x, y)returnsx * y.div(x, y)returnsx / yrounded toward zero, sodiv(7, 2) = 3anddiv(-7, 2) = -3.pow(x, y)returnsxto the powery: the product ofycopies ofx.pow(x, 0) = 1for everyx, including0.
Each argument is either an integer or another function call. An integer may be negative: its minus sign comes directly before its digits, as in -3. There are no decimal points. The whole string s can also be a single integer.
Examples
Example 1
s = "add(1,sub(1,0))"return = 2sub(1,0) = 1, then add(1,1) = 2.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 4 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.
More Google Onsite Interview questions
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 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
Similar questions at other companies
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Flatten a Nested ArrayFreshworks · ONSITE INTERVIEWFreshworks interview questions
- Flatten a Nested Object Into Dot KeysAdobe · PHONE SCREENAdobe interview questions
- Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Build an Expression Tree from PostfixPeopleStrong · PHONE SCREENPeopleStrong interview questions
- Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
- Ternary Expression to Binary TreeNutanix · ONSITE INTERVIEWNutanix interview questions
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
Google interview experiences
- Google L5 Security Engineer Domain Knowledge Round: 1 Experience
- Google Software Engineer Onsite Questions: 10 Experiences
- Google OA: What It Tests (2026)
- Google L4 SWE Phone Screen: 1 Experience
- Google ML Engineering Manager Onsite: 1 Experience
- Google Experienced SWE First Round and Onsite: 1 Experience