Basic Calculator with Multiplication and Parentheses
Updated
Problem statement
Evaluate a valid arithmetic expression containing nonnegative decimal integers, binary +, -, and *, parentheses, and optional spaces.
Multiplication has higher precedence than addition and subtraction. Parentheses override precedence. Return the integer result.
Function
calculate(expression: String) → intExamples
Example 1
expression = "2*(3+4)-5"return = 9The parenthesized sum is seven, multiplication gives fourteen, and subtracting five gives nine.
Example 2
expression = "12 + 3 * (4 - 2)"return = 18The multiplication contributes six before the outer addition.
Constraints
1 <= expression.length <= 10^5.- The expression is valid and contains only digits, spaces,
+,-,*,(, and). - Every operator is binary; unary signs are absent.
- All intermediate values fit in a signed 32-bit integer.
More Bloomberg LP Phone Screen questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Restricted JSON ParserOld Mission · PHONE SCREEN
- JSON ParserPatreon · PHONE SCREENPatreon interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Positive-Integer Expression EvaluationJane Street · PHONE SCREENJane Street interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Recursive Nested JSON Key SearchMongoDB · PHONE SCREENMongoDB interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)