Arithmetic Expression Evaluator
Updated
Problem statement
Evaluate a valid arithmetic expression containing multi-digit integers, spaces, parentheses, binary operators +, -, *, and /, and unary + or -.
Use standard precedence: unary signs first, then multiplication and division, then addition and subtraction. Operators at the same precedence are left-associative. Integer division truncates toward zero.
Return the expression's signed 64-bit integer value.
Function
evaluateExpression(expression: String) → longExamples
Example 1
expression = "-7 + 5 * 8 - 5 / 4 + (5 + 4)"return = 41Multiplication and division are evaluated before addition and subtraction: -7 + 40 - 1 + 9 = 41.
Example 2
expression = "2 * -(3 + 4) + 10 / 3"return = -11The parenthesized value is negated, and 10 / 3 truncates to 3, giving -14 + 3 = -11.
Constraints
1 <= expression.length <= 100000- Parentheses are nested at most 200 levels deep.
- The expression contains only digits, spaces, parentheses, and the operators
+,-,*, and/. - The expression is valid, every division has a nonzero divisor, and every intermediate result fits in a signed 64-bit integer.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
- Infer a Lexicographically Smallest Alien AlphabetPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Flatten JSON With Array IndicesAdobe · PHONE SCREENAdobe interview questions
- Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
- Evaluate an Arithmetic Expression with DivisionMotive · ONSITE INTERVIEWMotive interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Basic CalculatorAmazon · PHONE SCREEN · ONSITE INTERVIEW
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)