Evaluate arithmetic expressions with multiplication and integer division
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: Evaluate integer arithmetic with standard precedence and division truncated toward zero. Parse multi-digit literals and spaces, preserve left associativity, and avoid the negative-division error caused by floor rounding.
Read the full Amazon Software Engineer interview experience this question came from
Constraints
- The expression has between 1 and 100000 characters.
- Integer literals are between 0 and 1000000.
- Every intermediate term and cumulative sum lies in signed 32-bit range.
Examples
Input: ('3 + 2 * 2',)
Expected Output: 7
Explanation: Multiplication is evaluated before addition.
Input: ('0 - 3 / 2',)
Expected Output: -1
Explanation: The negative term truncates toward zero.
Hints
- An addition or subtraction separates two multiplication and division chains.
- When dividing a negative term, rounding toward zero differs from flooring.