PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement expression parsing and evaluation, assessing competencies in string parsing, operator precedence, parentheses handling, and integer arithmetic semantics including truncation toward zero.

  • medium
  • Instacart
  • Coding & Algorithms
  • Software Engineer

Evaluate an arithmetic expression

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Write a function that evaluates a mathematical expression given as a string. Requirements: - Supports non-negative integers and whitespace. - Supports operators `+`, `-`, `*`, `/` with standard precedence (`*` and `/` before `+` and `-`). - Optionally supports parentheses `(` and `)` (if you choose to support them, document your approach). - Integer division should truncate toward zero. Examples: - Input: `"3+2*2"` → Output: `7` - Input: `" 3/2 "` → Output: `1` - Input: `" 3+5 / 2 "` → Output: `5` - (If supporting parentheses) Input: `"2*(3+(4-1))"` → Output: `12`

Quick Answer: This question evaluates a candidate's ability to implement expression parsing and evaluation, assessing competencies in string parsing, operator precedence, parentheses handling, and integer arithmetic semantics including truncation toward zero.

Write a function that evaluates a mathematical expression given as a string. The expression may contain non-negative integers, whitespace, the operators '+', '-', '*', and '/', and parentheses '()'. Multiplication and division must be evaluated before addition and subtraction, unless parentheses change the order. Division must truncate toward zero. You may assume the expression is valid and never divides by zero. This version of the problem supports parentheses.

Constraints

  • 1 <= len(s) <= 100000
  • s contains digits, spaces, '+', '-', '*', '/', '(', and ')'
  • All numbers in the expression are non-negative integers
  • The expression is valid and division by zero will not occur
  • The final result fits in a 32-bit signed integer

Examples

Input: "3+2*2"

Expected Output: 7

Explanation: Multiplication happens first: 2*2 = 4, then 3+4 = 7.

Input: " 3/2 "

Expected Output: 1

Explanation: 3 divided by 2 truncates toward zero, so the result is 1.

Input: " 3+5 / 2 "

Expected Output: 5

Explanation: 5/2 = 2 after truncation, then 3+2 = 5.

Input: "2*(3+(4-1))"

Expected Output: 12

Explanation: Inside the parentheses, 4-1 = 3, then 3+3 = 6, and finally 2*6 = 12.

Input: "42"

Expected Output: 42

Explanation: A single number evaluates to itself.

Input: "(2+6*3+5-(3*14/7+2)*5)+3"

Expected Output: -12

Explanation: Evaluating step by step gives (2+18+5-(6+2)*5)+3 = (25-40)+3 = -12.

Input: "7/(2-5)"

Expected Output: -2

Explanation: 2-5 = -3, and 7/(-3) truncates toward zero to -2.

Input: "14-3/2"

Expected Output: 13

Explanation: 3/2 = 1 after truncation, so 14-1 = 13.

Hints

  1. Use one stack for numbers and another for operators.
  2. When you read a new operator, apply any earlier operators that have higher or equal precedence. Parentheses act as barriers.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Largest Adjacent Stock Price Change - Instacart (medium)
  • Implement an In-Memory File Storage System - Instacart (medium)
  • Decode an encoded string - Instacart (medium)
  • Implement worker time and payroll tracker - Instacart (hard)
  • Solve Two Sorted-Array Tasks - Instacart (hard)