Quick 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.

Evaluate arithmetic expressions with multiplication and integer division

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Evaluate a valid arithmetic expression containing nonnegative integer literals, binary `+`, `-`, `*`, `/`, and optional spaces between tokens. Multiplication and division take precedence over addition and subtraction. Operations at the same precedence associate left to right. Division truncates toward zero. ### Function Signature ```python def evaluate_expression(expression: str) -> int: ``` ### Rules There are no parentheses or unary operators. Spaces do not split the digits of a number. Every divisor is nonzero. Return the integer value without using an expression-evaluation library. These rules specify the basic-calculator-style exercise. ### 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 ```text Input: expression = "3 + 2 * 2" Output: 7 ``` Multiplication is performed before addition. ```text Input: expression = "0 - 3 / 2" Output: -1 ``` The division result contributes a negative term truncated toward zero.

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

Evaluate a valid arithmetic expression containing nonnegative integer literals, binary `+`, `-`, `*`, `/`, and optional spaces between tokens. Multiplication and division take precedence over addition and subtraction. Operations at the same precedence associate left to right. Division truncates toward zero. ### Function Signature ```python def evaluate_expression(expression: str) -> int: ``` ### Rules There are no parentheses or unary operators. Spaces do not split the digits of a number. Every divisor is nonzero. Return the integer value without using an expression-evaluation library. These rules specify the basic-calculator-style exercise. ### 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 ```text Input: expression = "3 + 2 * 2" Output: 7 ``` Multiplication is performed before addition. ```text Input: expression = "0 - 3 / 2" Output: -1 ``` The division result contributes a negative term truncated toward zero.

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

  1. An addition or subtraction separates two multiplication and division chains.
  2. When dividing a negative term, rounding toward zero differs from flooring.

Evaluate arithmetic expressions with multiplication and integer division

hardAmazonSoftware Engineer

Evaluate a valid arithmetic expression containing nonnegative integer literals, binary +, -, *, /, and optional spaces between tokens. Multiplication and division take precedence over addition and subtraction. Operations at the same precedence associate left to right. Division truncates toward zero.

Function Signature

def evaluate_expression(expression: str) -> int:

Rules

There are no parentheses or unary operators. Spaces do not split the digits of a number. Every divisor is nonzero. Return the integer value without using an expression-evaluation library. These rules specify the basic-calculator-style exercise.

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: expression = "3 + 2 * 2"
Output: 7

Multiplication is performed before addition.

Input: expression = "0 - 3 / 2"
Output: -1

The division result contributes a negative term truncated toward zero.

Examples

Example 1
Input
('3 + 2 * 2',)
Output
7
Notes
Multiplication is evaluated before addition.
Example 2
Input
('0 - 3 / 2',)
Output
-1
Notes
The negative term truncates toward zero.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def evaluate_expression(expression) -> int:
Sample call
evaluate_expression('3 + 2 * 2')
Language
Case 1
Input
('3 + 2 * 2',)
Expected
7
Case 2
Input
('0 - 3 / 2',)
Expected
-1

Browse More Questions

Show the approach

Approach

Maintain a cumulative total for completed additive terms, a signed current term, and the operator before the literal being read. Parse each decimal literal from left to right. For multiplication and division, update the current term immediately. For addition and subtraction, commit the previous term to the total and begin a new positive or negative term. At the end, apply the last pending operator and commit the last term.

The term only combines adjacent multiplication and division operations, in input order, so precedence and left associativity hold. Addition and subtraction then combine the completed signed terms in their input order. Signed truncation is preserved by taking the quotient of the absolute magnitude and restoring its sign in Python, and by the corresponding signed division operation in the other implementations.

Time complexity:
O(n)
Space complexity:
O(1)