FastPrepCalculator with Parentheses
Problems/Calculator with Parentheses

Calculator with Parentheses

Google logoGoogle● MediumNEW GRADINTERNPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report asks for arithmetic evaluation with parentheses. The practice grammar fixes supported operators, unary signs, whitespace, and integer semantics.

Evaluate a valid integer expression containing non-negative decimal integers, ASCII spaces, binary +, -, *, and /, unary + and -, and balanced parentheses.

Parentheses have the highest grouping priority, multiplication and division have higher precedence than addition and subtraction, and binary operators with the same precedence are evaluated from left to right. Integer division truncates toward zero, and the expression never divides by zero.

Every parsed integer, intermediate value, and final result fits in a signed 32-bit integer. Return the final result.

Function

calculateExpression(expression: String) → int

Examples

Example 1

expression = "1 + (2 - 3) + 4"return = 4

The parenthesized subexpression evaluates to -1.

Example 2

expression = "-(10 - (3 + 2))"return = -5

Unary minus applies to the complete parenthesized expression.

Constraints

  • 1 <= expression.length <= 10^5
  • The expression is valid and parentheses are balanced.
  • The final result and every parsed integer fit in signed 32-bit range.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int calculateExpression(String expression) {
        // Write your code here.
    }
}
Checking account…