Arithmetic Expression Calculator
Updated
Problem statement
Evaluate the valid integer arithmetic expression expression and return its value. Do not use a built-in expression evaluator.
The expression contains decimal non-negative integer literals, binary + and -, parentheses, and ordinary ASCII spaces. Calculate addition and subtraction from left to right. Calculate each expression inside parentheses as a group.
A number or a group in parentheses may have one or more + or - signs before it. Spaces may appear between these signs. Each minus sign reverses the value’s sign; each plus sign leaves it unchanged. For example, 1--2 is 3 and --(4) is 4. Spaces may appear between tokens but never split the digits of one literal.
Multiplication, division, variables, invalid input and error reporting are outside this exercise. The final answer fits a signed 32-bit integer; use wider arithmetic for intermediate values.
Function
calculate(expression: String) → intExamples
Example 1
expression = "1 + 2 - 3"return = 0Addition and subtraction are evaluated left to right.
Example 2
expression = "-(2 + 3) + 10"return = 5The leading unary minus negates the complete parenthesized value.
Example 3
expression = "1 - -2 + -(3 - 5)"return = 5A binary minus followed by a unary minus adds 2; negating the final group adds another 2.
Constraints
1 <= expression.length <= 2000.- The expression obeys the stated grammar, has at least one literal, and uses only digits, spaces,
+,-,(and). - Every literal has numeric value in
[0, 10^9]; leading zeroes are allowed. - Parentheses are properly matched and nesting depth is at most
100. - The final value is in
[-2147483648, 2147483647]. All intermediate values fit signed64-bit arithmetic under these length/literal bounds.
More Snowflake Phone Screen questions
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Evaluate a Parenthesized Integer ExpressionAdobe · PHONE SCREENAdobe interview questions
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Decode an Encoded StringAmazon · PHONE SCREEN · ONSITE INTERVIEW