Evaluate an Infix Expression with Parentheses by Converting It to Postfix

Quick Overview

Evaluate an infix arithmetic expression with non-negative integers, spaces, parentheses and the operators +, -, * and /, where integer division truncates toward zero. It tests tokenizing multi-digit numbers, converting infix to postfix with an operator stack, and evaluating the postfix form with a value stack.

Evaluate an Infix Expression with Parentheses by Converting It to Postfix

Company: Elastic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `expression` that holds a valid infix arithmetic expression with non-negative integers, the operators `+`, `-`, `*`, `/`, and parentheses `(` `)`, convert the expression to postfix (Reverse Polish) notation and evaluate it. Integer division truncates toward zero. The expression is guaranteed to be valid and contains no division by zero. Return the integer result of evaluating the expression. ### Function Signature ```python def evaluate_expression(expression: str) -> int: ... ``` ### Rules - Spaces may appear between tokens and are ignored. A number is a run of consecutive digits, so `12` is twelve. - Every operator is binary. There is no unary minus or unary plus. Numbers in the input are non-negative, but intermediate results can be negative. - `*` and `/` bind tighter than `+` and `-`. Operators of equal precedence group left to right, so `10 - 4 - 3` is `(10 - 4) - 3`. - `/` truncates toward zero: `7 / 2` is `3` and `-7 / 2` is `-3`. ### Constraints - `1 <= expression.length <= 10^4` - `expression` contains only digits, `+`, `-`, `*`, `/`, `(`, `)`, and spaces. - `expression` is a valid infix expression. - No division by zero occurs. - All intermediate and final results fit in a 32-bit signed integer. ### Examples ```text Input: expression = "3 + 5 * 2 - 8 / 4" Output: 11 ``` Multiplication and division come first: `5 * 2 = 10` and `8 / 4 = 2`, then `3 + 10 - 2 = 11`. The postfix form is `3 5 2 * + 8 4 / -`. ```text Input: expression = "(1 + 2) * (3 + 4)" Output: 21 ``` The parentheses force both additions first: `1 + 2 = 3` and `3 + 4 = 7`, then `3 * 7 = 21`. ```text Input: expression = "(2 - 9) / 2 * 3" Output: -9 ``` `2 - 9 = -7`, and `-7 / 2` is `-3.5`, which truncates toward zero to `-3`. `/` and `*` have equal precedence and group left to right, so the result is `-3 * 3 = -9`.

Overview: Evaluate an infix arithmetic expression with non-negative integers, spaces, parentheses and the operators +, -, * and /, where integer division truncates toward zero. It tests tokenizing multi-digit numbers, converting infix to postfix with an operator stack, and evaluating the postfix form with a value stack.

Elastic logo
Elastic
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string expression that holds a valid infix arithmetic expression with non-negative integers, the operators +, -, *, /, and parentheses ( ), convert the expression to postfix (Reverse Polish) notation and evaluate it.

Integer division truncates toward zero. The expression is guaranteed to be valid and contains no division by zero.

Return the integer result of evaluating the expression.

Function Signature

def evaluate_expression(expression: str) -> int:
    ...

Rules

  • Spaces may appear between tokens and are ignored. A number is a run of consecutive digits, so 12 is twelve.
  • Every operator is binary. There is no unary minus or unary plus. Numbers in the input are non-negative, but intermediate results can be negative.
  • * and / bind tighter than + and - . Operators of equal precedence group left to right, so 10 - 4 - 3 is (10 - 4) - 3 .
  • / truncates toward zero: 7 / 2 is 3 and -7 / 2 is -3 .

Constraints

  • 1 <= expression.length <= 10^4
  • expression contains only digits, + , - , * , / , ( , ) , and spaces.
  • expression is a valid infix expression.
  • No division by zero occurs.
  • All intermediate and final results fit in a 32-bit signed integer.

Examples

Input: expression = "3 + 5 * 2 - 8 / 4"
Output: 11

Multiplication and division come first: 5 * 2 = 10 and 8 / 4 = 2, then 3 + 10 - 2 = 11. The postfix form is 3 5 2 * + 8 4 / -.

Input: expression = "(1 + 2) * (3 + 4)"
Output: 21

The parentheses force both additions first: 1 + 2 = 3 and 3 + 4 = 7, then 3 * 7 = 21.

Input: expression = "(2 - 9) / 2 * 3"
Output: -9

2 - 9 = -7, and -7 / 2 is -3.5, which truncates toward zero to -3. / and * have equal precedence and group left to right, so the result is -3 * 3 = -9.