FastPrepEvaluate a Nested Math Expression
Problems/Evaluate a Nested Math Expression

Evaluate a Nested Math Expression

Google logoGoogle● MediumFULLTIMENEW GRADINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionExamples

Problem statement

Source note: Reported in several Google interviews: onsite and phone-screen rounds for full-time, new-grad and PhD intern software engineer roles. Earlier reports describe nested add and sub calls on integers, with optional spaces. A late-2026 intern phone-screen report adds mul, div and pow, allows negative numbers, and says only whole numbers were needed.

No report says how div rounds, whether a divisor can be 0, whether pow can get a negative exponent, or what pow(0, 0) is. We chose these rules: div rounds toward zero, no divisor is 0, no exponent is below 0, pow(0, 0) is 1, and every call's value fits in a 32-bit signed integer. We also chose the length limit and the new examples.

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

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) returns x + y.
  • sub(x, y) returns x - y.
  • mul(x, y) returns x * y.
  • div(x, y) returns x / y rounded toward zero, so div(7, 2) = 3 and div(-7, 2) = -3.
  • pow(x, y) returns x to the power y: the product of y copies of x. pow(x, 0) = 1 for every x, including 0.

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.

The problem statement continues
Pro

Examples

Example 1

s = "add(1,sub(1,0))"return = 2

sub(1,0) = 1, then add(1,1) = 2.

02 · Example 2
FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

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
All Google interview questionsSee Google hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week