PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers

Quick Overview

This question evaluates proficiency in parsing arithmetic expressions, handling unary and binary operators, parentheses, whitespace, numeric tokenization, and robust syntactic validation.

  • Uber
  • Coding & Algorithms
  • Software Engineer

Evaluate an Arithmetic Expression

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Interview Round: Onsite

Implement an expression evaluator for a simplified calculator. Given a string `s`, return the integer value of the expression. The expression may contain: - Non-negative integers - The binary operators `+` and `-` - Parentheses `(` and `)` - Arbitrary spaces Unary signs may appear at the beginning of the expression or immediately after an opening parenthesis, such as `-3 + (2 - 5)`. Examples: ```text Input: "1 + 2 - 3" Output: 0 Input: "(1 + (4 - 2)) - 3" Output: 0 Input: "-2 + (3 - 1)" Output: 0 ``` Follow-up: add string validation. Before evaluating, verify that the expression is syntactically valid. Reject expressions with invalid characters, mismatched parentheses, missing operands, consecutive binary operators, or malformed numbers. Define and document how your function reports invalid input, for example by returning an error or throwing an exception.

Quick Answer: This question evaluates proficiency in parsing arithmetic expressions, handling unary and binary operators, parentheses, whitespace, numeric tokenization, and robust syntactic validation.

Part 1: Evaluate a Simplified Arithmetic Expression

Given a syntactically valid string s representing an arithmetic expression, return its integer value. The expression may contain non-negative integers, the binary operators '+' and '-', parentheses '(' and ')', and arbitrary spaces. A unary '+' or '-' may appear at the start of the expression or immediately after an opening parenthesis, and it applies to the next number or parenthesized group. You may assume the input is always valid.

Constraints

  • 1 <= len(s) <= 100000
  • s contains only digits, spaces, '+', '-', '(' and ')'
  • The expression is guaranteed to be syntactically valid
  • The final answer fits in a signed 64-bit integer

Examples

Input: ("0",)

Expected Output: 0

Explanation: A single number evaluates to itself.

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

Expected Output: 3

Explanation: Ignoring spaces, 2 - 1 + 2 = 3.

Input: ("(1+(4+5+2)-3)+(6+8)",)

Expected Output: 23

Explanation: The left group is 1 + 11 - 3 = 9, and the right group is 14, so the total is 23.

Input: ("-(-42)",)

Expected Output: 42

Explanation: The inner value is -42, and negating it gives 42.

Input: ("-(3-(2-1))",)

Expected Output: -2

Explanation: First compute (2 - 1) = 1, then 3 - 1 = 2, and finally apply the outer unary minus.

Input: ("( -7 )",)

Expected Output: -7

Explanation: The unary minus appears immediately after '(', so the value inside the parentheses is -7.

Input: ("+(1-(+2-3)+(4-(5-6)))",)

Expected Output: 7

Explanation: Compute (+2 - 3) = -1 and (5 - 6) = -1, so the full expression becomes 1 - (-1) + (4 - (-1)) = 2 + 5 = 7.

Hints

  1. Keep a running result and a current sign for the number you are building.
  2. When you enter parentheses, save the outer result and sign on a stack so you can restore them after evaluating the inner expression.

Part 2: Validate and Evaluate an Arithmetic Expression

Given a string s, first verify whether it is a syntactically valid arithmetic expression, then evaluate it. The expression may use digits, spaces, '+', '-', '(' and ')'. A unary '+' or '-' is allowed only at the very beginning of the expression or immediately after an opening parenthesis, and it applies to the next number or parenthesized group. Numbers are contiguous digit sequences; leading zeros are allowed. Reject invalid input that has invalid characters, mismatched parentheses, empty parentheses, missing operands, two operands without an operator between them, or consecutive operators used where a unary sign is not allowed. Return the integer value for a valid expression; otherwise return the exact string "ERROR".

Constraints

  • 0 <= len(s) <= 100000
  • s may contain arbitrary characters, but only digits, spaces, '+', '-', '(' and ')' can appear in a valid expression
  • Unary '+' and '-' are allowed only at the start of the whole expression or immediately after '('
  • If the expression is valid, the final answer fits in a signed 64-bit integer

Examples

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

Expected Output:

Explanation: This expression is valid and evaluates to 0.

Input: -(3 - 1) + 5

Expected Output:

Explanation: A unary minus at the start may apply to a parenthesized group.

Input: 1 +- 2

Expected Output: ERROR

Explanation: After a binary '+', another operator is not allowed here.

Input: (2 + 3

Expected Output: ERROR

Explanation: The opening parenthesis is never closed.

Input:

Expected Output: ERROR

Explanation: Edge case: an empty expression containing only spaces is invalid.

Hints

  1. Track what kind of token is expected next: an operand, or an operator/closing parenthesis.
  2. You can validate and evaluate in one pass by combining a stack for parentheses with a small state machine.
Last updated: May 12, 2026

Loading coding console...

PracHub

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

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • 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

  • Implement Store Autocomplete - Uber (medium)
  • Schedule Non-Overlapping Meetings Efficiently - Uber (hard)
  • Compute CDF from a PDF Function - Uber (medium)
  • Find the First Unique IP - Uber (medium)
  • Count Valid Infection Orders - Uber (medium)