FastPrepPlus and Minus Calculator
Problems/Plus and Minus Calculator

Plus and Minus Calculator

Indeed logoIndeed● MediumINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A Karat interview's coding part built a small calculator in two steps: first + and - over positive whole numbers, as in 100+200+300 or 255, then parentheses on top of the same code.

The report does not say how signs work or give any limits. Here, one function covers both parts: the input may contain parentheses, every + and - sits between two operands with no unary minus and no spaces, and the answer can be negative. Numbers go up to 10^6, the expression has at most 10^4 characters, and parentheses nest at most 100 deep. These limits are practice choices.

The judged task matches the report at about 90%.

Build a simple calculator. expression is made of positive whole numbers, the operators + and -, and parentheses. Return its value.

  • Operators are applied from left to right. For example, 10-4-3 means (10-4)-3, which is 3.
  • A part inside parentheses is worked out first and then used as a single number. For example, 10-(4-3) is 9.
  • The answer can be negative.

An operand is a number or a part in parentheses. Every + and - stands between two operands, so there is no unary sign: inputs such as -5 or 2+-3 never appear. There are no spaces.

Function

calculate(expression: String) → int

Examples

Example 1

expression = "100+200+300"return = 600

Add from left to right: 100 + 200 = 300, then 300 + 300 = 600.

Example 2

expression = "255"return = 255

A single number is its own value.

Example 3

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

Work from the inside out: 9-4 = 5, then 2-5 = -3, then 7-(-3) = 10, and finally 10+1 = 11.

Example 4

expression = "15-(5+12)"return = -2

The parentheses give 5+12 = 17, and 15-17 = -2. The answer can be negative.

Constraints

  • 1 <= expression.length <= 10^4
  • expression contains only digits, +, -, ( and ).
  • Each number is from 1 to 10^6 and has no leading zeros.
  • expression is valid: every operator has an operand on each side, and every pair of parentheses is matched and holds an expression.
  • Parentheses nest at most 100 deep.

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public int calculate(String expression) {
        // Write your code here.
    }
}
Checking account…