FastPrepParenthesized String Calculator
Problems/Parenthesized String Calculator

Parenthesized String Calculator

Meta logoMeta● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report identifies a string-calculator task comparable to the familiar parenthesized addition/subtraction exercise. The judged grammar makes the unnamed variant deterministic and matches that recognizable seed at about 84%.

Evaluate a valid arithmetic expression containing nonnegative decimal integers, binary + and -, parentheses, and optional spaces.

Return the integer result. Every operator has an operand on both sides, parentheses are balanced, and all intermediate values fit signed 32-bit range.

Function

calculate(expression: String) → int

Examples

Example 1

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

The parenthesized term equals -1.

Example 2

expression = "12-(4+(2-1))"return = 7

The inner expression is 1, so the grouped value is 5.

Example 3

expression = "42"return = 42

A single literal evaluates to itself.

Constraints

  • 1 <= expression.length <= 10^5.
  • The expression grammar is valid and uses only digits, spaces, +, -, (, and ).
  • Operators are binary; unary signs are not present.

More Meta problems

  • Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
  • Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
  • Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public int calculate(String expression) {
        // Write your solution here.
    }
}
Checking account…