PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Evaluate arithmetic expression without parentheses evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Evaluate arithmetic expression without parentheses

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Write evaluate(s: string) to compute the value of an arithmetic expression containing non-negative integers, '+', '-', '*', '/', and spaces, with operator precedence (* and / before + and -) and no parentheses. Division truncates toward zero. Aim for a single pass with O( 1) extra space beyond the output and a small stack or running registers. Discuss handling multi-digit numbers, consecutive spaces, and edge cases such as '3/2', '14-3/2', and trailing operators being invalid. Provide unit tests.

Quick Answer: Evaluate arithmetic expression without parentheses evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Implement `evaluate(s)` to compute the value of an arithmetic expression string containing non-negative integers, the operators `+`, `-`, `*`, `/`, and spaces. Operators follow normal precedence: `*` and `/` bind tighter than `+` and `-`. There are no parentheses. Integer division truncates toward zero (e.g. `3/2 == 1`). Process the string in a single left-to-right pass. Track the current multi-digit number being built and the operator that precedes it. When you reach the next operator (or the end of the string), commit the pending number: push it for `+`/`-` (signed), or fold it into the top of the stack for `*`/`/`. The answer is the sum of all stack entries. This keeps the multiplicative work local so precedence falls out naturally without a separate parse tree. Examples: - `"3+2*2"` -> `7` - `" 3/2 "` -> `1` - `"14-3/2"` -> `13` - `"2-3*4"` -> `-10` You may assume the expression is valid (a number after every operator, no trailing operator); trailing-operator inputs are considered invalid and are out of scope.

Constraints

  • 1 <= len(s); the expression is non-empty and represents a valid expression.
  • s contains only digits, '+', '-', '*', '/', and ' ' (space).
  • All integers in the expression are non-negative; intermediate and final results fit in a 32-bit signed integer.
  • Integer division truncates toward zero (3/2 == 1, and would give -1 for a negative quotient).
  • No parentheses; the input has a number after every operator (trailing operators are invalid and out of scope).

Examples

Input: ("3+2*2",)

Expected Output: 7

Explanation: * binds tighter: 2*2=4, then 3+4=7.

Input: (" 3/2 ",)

Expected Output: 1

Explanation: Leading/trailing spaces ignored; 3/2 truncates toward zero to 1.

Input: (" 3+5 / 2 ",)

Expected Output: 5

Explanation: 5/2=2 first, then 3+2=5; interior spaces around the operator are skipped.

Input: ("14-3/2",)

Expected Output: 13

Explanation: 3/2=1 (truncated), then 14-1=13.

Input: ("42",)

Expected Output: 42

Explanation: A single multi-digit number with no operator.

Input: ("1-1+1",)

Expected Output: 1

Explanation: Left-to-right for equal-precedence + and -: 1-1=0, 0+1=1.

Input: ("0",)

Expected Output: 0

Explanation: Zero edge case.

Input: ("100*2/25",)

Expected Output: 8

Explanation: Chained * and /: 100*2=200, 200/25=8.

Input: ("2-3*4",)

Expected Output: -10

Explanation: 3*4=12, then 2-12=-10 — negative intermediate result is fine.

Input: (" 30 ",)

Expected Output: 30

Explanation: Multiple leading and trailing spaces around a single number.

Hints

  1. Walk the string once. Build the current number digit by digit (num = num*10 + digit) so multi-digit numbers and runs of spaces are handled naturally.
  2. Remember the operator that came BEFORE the current number. Only act when you hit the next operator (or the final character): that's when the pending number is complete.
  3. Defer + and - by pushing the signed number onto a stack; apply * and / immediately to the top of the stack. The final answer is the sum of the stack — precedence is handled for free.
  4. For truncate-toward-zero division, use int(a / b) in Python (Python's // floors, which differs for negatives). Java, C++, and JS Math.trunc already truncate toward zero.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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 Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)