Skip to content

Evaluate Mini Calculator Functions

Published 3 October 2026

A popular Google and Uber coding question.

Problem statement

A small calculator app supports only two functions:

  • add(x, y): returns x + y
  • sub(x, y): returns x - y

The input is a string expression that may contain nested function calls. The expression may also contain:

  • integers
  • negative integers
  • spaces
  • commas
  • parentheses

The expression is always valid. Return the final integer result. For example add(add(1,3), sub(1,3)) means (1 + 3) + (1 - 3). So the result is 2

You are given expression: a valid string expression. Return the evaluated integer result.

Rules:

  • add(x, y) returns x + y
  • sub(x, y) returns x - y
  • Expressions can be nested
  • Integers may be negative
  • Spaces should be ignored
  • The input expression is always valid

Examples:

Input:
expression = "add(add(1,3), sub(1,3))"

Output:
2
Input:
expression = "sub(1,3)"

Output:
-2