Evaluate Nested Binary Arithmetic Function Calls

Quick Overview

Evaluate nested add, sub, mul, div, and pow expressions with recursive parsing, negative integers, whitespace, explicit division rules, and validation.

Evaluate Nested Binary Arithmetic Function Calls

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Evaluate a string of nested calls to `add`, `sub`, `mul`, `div`, and `pow`. Every function has exactly two arguments. Arguments may be integer literals, including negative integers, or nested calls. Whitespace is allowed between tokens. For example, `add(5,mul(2,pow(5,2)))` evaluates to `55`. ### Constraints & Assumptions Decimal literals are outside the task. The source does not define nonintegral division, negative exponents, overflow, or malformed input behavior. For the implementation below, agree on integer division truncated toward zero, nonnegative exponents, arbitrary-precision integers, and errors for invalid syntax or division by zero. These are explicit practice assumptions. ### Clarifying Questions to Ask - Should `div(-7,2)` truncate toward zero or use another rule? - Are negative exponents excluded, and what should `pow(0,0)` return? - Is input guaranteed syntactically valid, or should parsing reject trailing tokens? ```hint Let parsing return a value One recursive call can consume exactly one expression and leave the cursor at the next delimiter. ``` ### What a Strong Answer Covers - A recursive grammar for integer literals and binary function calls. - Cursor advancement, whitespace skipping, commas, and parentheses. - Correct operand order for subtraction, division, and exponentiation. - Explicit arithmetic-domain errors and full-input consumption. ### Follow-up Questions - How would you support decimal literals without changing the nesting grammar? - How would you prevent deeply nested input from exhausting the call stack?

Overview: Evaluate nested add, sub, mul, div, and pow expressions with recursive parsing, negative integers, whitespace, explicit division rules, and validation.

Google logo
Google
Nov 20, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Evaluate a string of nested calls to add, sub, mul, div, and pow. Every function has exactly two arguments. Arguments may be integer literals, including negative integers, or nested calls. Whitespace is allowed between tokens.

For example, add(5,mul(2,pow(5,2))) evaluates to 55.

Constraints & Assumptions

Decimal literals are outside the task. The source does not define nonintegral division, negative exponents, overflow, or malformed input behavior. For the implementation below, agree on integer division truncated toward zero, nonnegative exponents, arbitrary-precision integers, and errors for invalid syntax or division by zero. These are explicit practice assumptions.

Clarifying Questions to Ask Guidance

  • Should div(-7,2) truncate toward zero or use another rule?
  • Are negative exponents excluded, and what should pow(0,0) return?
  • Is input guaranteed syntactically valid, or should parsing reject trailing tokens?

What a Strong Answer Covers Guidance

  • A recursive grammar for integer literals and binary function calls.
  • Cursor advancement, whitespace skipping, commas, and parentheses.
  • Correct operand order for subtraction, division, and exponentiation.
  • Explicit arithmetic-domain errors and full-input consumption.

Follow-up Questions Guidance

  • How would you support decimal literals without changing the nesting grammar?
  • How would you prevent deeply nested input from exhausting the call stack?