PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Bytedance

Build a validated add-sub calculator

Last updated: Apr 6, 2026

Quick Overview

This question evaluates skills in string parsing, tokenization, input validation, and safe integer arithmetic handling, with emphasis on enforcing format rules such as operator placement, prevention of leading zeros, and 32-bit signed integer bounds.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Build a validated add-sub calculator

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a string `expression`, write a function that validates and evaluates it. The expression represents arithmetic using only non-negative integers and the operators `+` and `-`. A valid expression must satisfy all of the following: - It contains only digits, `+`, and `-`. - It does not contain spaces, parentheses, or any other characters. - It consists of numbers separated by binary operators only, so it cannot start or end with an operator. - Two operators cannot appear next to each other. - A number cannot have leading zeros unless the number is exactly `0`. - Every parsed number must fit in the 32-bit signed integer range. - The final evaluated result must also fit in the 32-bit signed integer range. If the expression is invalid, report it as invalid. Otherwise, return the computed result. Examples of invalid inputs include: - `01+2` because `01` has a leading zero - `1++2` because two operators are adjacent - `+1-2` because the expression starts with an operator - `12a-3` because it contains an invalid character Examples of valid inputs include: - `0` - `12-3+4` - `7+0-5` Implement the parser/evaluator and make sure edge cases such as the standalone value `0` are handled correctly.

Quick Answer: This question evaluates skills in string parsing, tokenization, input validation, and safe integer arithmetic handling, with emphasis on enforcing format rules such as operator placement, prevention of leading zeros, and 32-bit signed integer bounds.

Related Interview Questions

  • Reverse Nodes in K-Sized Groups - Bytedance
  • Solve Bracket Matching and Tree Width - Bytedance (hard)
  • Reverse Linked List Groups - Bytedance (medium)
  • Solve Stack and String Shift Problems - Bytedance (medium)
  • Find LCA With Parent Pointers - Bytedance (medium)
Bytedance logo
Bytedance
Feb 18, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0

Given a string expression, write a function that validates and evaluates it.

The expression represents arithmetic using only non-negative integers and the operators + and -.

A valid expression must satisfy all of the following:

  • It contains only digits, + , and - .
  • It does not contain spaces, parentheses, or any other characters.
  • It consists of numbers separated by binary operators only, so it cannot start or end with an operator.
  • Two operators cannot appear next to each other.
  • A number cannot have leading zeros unless the number is exactly 0 .
  • Every parsed number must fit in the 32-bit signed integer range.
  • The final evaluated result must also fit in the 32-bit signed integer range.

If the expression is invalid, report it as invalid. Otherwise, return the computed result.

Examples of invalid inputs include:

  • 01+2 because 01 has a leading zero
  • 1++2 because two operators are adjacent
  • +1-2 because the expression starts with an operator
  • 12a-3 because it contains an invalid character

Examples of valid inputs include:

  • 0
  • 12-3+4
  • 7+0-5

Implement the parser/evaluator and make sure edge cases such as the standalone value 0 are handled correctly.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Bytedance•More Software Engineer•Bytedance Software Engineer•Bytedance Coding & Algorithms•Software Engineer Coding & Algorithms
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.