PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates string manipulation and parsing skills for numeric computation, covering addition of integers represented as character arrays and correct evaluation of arithmetic expressions with operator precedence.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Solve String Addition and Expression Evaluation

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

The interview included two coding problems: 1. **Add numbers from character arrays** - You are given two non-empty character arrays, where each array represents a non-negative integer in decimal form. - Each element is a digit character such as `'0'` through `'9'`. - Return the sum as a string. - You may not convert the entire input directly into a built-in integer type. Example: - Input: `['1','2','3']`, `['9','9']` - Output: `"222"` 2. **Evaluate a basic arithmetic expression** - You are given a string expression containing non-negative integers, spaces, and the operators `+`, `-`, `*`, and `/`. - Evaluate the expression and return the integer result. - Operator precedence should be respected: multiplication and division before addition and subtraction. - Integer division truncates toward zero. - The expression is guaranteed to be valid and contains no parentheses. Example: - Input: `"3+2*2"` - Output: `7` - Input: `" 14-3/2 "` - Output: `13`

Quick Answer: This question evaluates string manipulation and parsing skills for numeric computation, covering addition of integers represented as character arrays and correct evaluation of arithmetic expressions with operator precedence.

Part 1: Add Two Numbers from Character Arrays

You are given two non-empty arrays of digit characters. Each array represents a non-negative integer in decimal form, with one digit per element. Return their sum as a string. You may not convert the entire array directly into a built-in integer type or use big integer libraries. Instead, simulate addition the same way you would do it by hand from right to left.

Constraints

  • 1 <= len(digits1), len(digits2) <= 100000
  • Each element of digits1 and digits2 is a character from '0' to '9'
  • The input numbers have no leading zeros unless the number itself is zero
  • You must not convert the entire input into a built-in integer type

Examples

Input: (['1', '2', '3'], ['9', '9'])

Expected Output: "222"

Explanation: 123 + 99 = 222.

Input: (['9', '9', '9'], ['1'])

Expected Output: "1000"

Explanation: A carry propagates through all digits.

Input: (['0'], ['0'])

Expected Output: "0"

Explanation: Edge case: both numbers are zero.

Input: (['5'], ['5'])

Expected Output: "10"

Explanation: Single-digit addition that creates a new leading digit.

Hints

  1. Start from the last digit of each array and move left, just like grade-school addition.
  2. Keep track of a carry value after adding each pair of digits.

Part 2: Evaluate a Basic Arithmetic Expression

You are given a valid string expression containing non-negative integers, spaces, and the operators '+', '-', '*', and '/'. Evaluate the expression and return the integer result. The expression contains no parentheses. Standard operator precedence must be respected: multiplication and division are performed before addition and subtraction. Division truncates toward zero.

Constraints

  • 1 <= len(expression) <= 100000
  • The expression contains only digits, spaces, and the operators '+', '-', '*', '/'
  • The expression is guaranteed to be valid and contains no parentheses
  • Division by zero will not occur
  • Integer division must truncate toward zero

Examples

Input: "3+2*2"

Expected Output: 7

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

Input: " 14-3/2 "

Expected Output: 13

Explanation: 3 / 2 truncates toward zero to become 1, so 14 - 1 = 13.

Input: "42"

Expected Output: 42

Explanation: Edge case: the expression is just a single number.

Input: " 3+5 / 2 "

Expected Output: 5

Explanation: 5 / 2 truncates toward zero to 2, so 3 + 2 = 5.

Input: "0-3/2"

Expected Output: -1

Explanation: 3 / 2 becomes 1, so 0 - 1 = -1.

Hints

  1. You do not need a full parser. Process the string left to right while remembering the previous operator.
  2. A stack is a simple way to handle precedence: push numbers for + and -, but immediately combine for * and /.
Last updated: May 7, 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
  • 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

  • Minimize Increments to Equalize Path Costs - Bytedance (medium)
  • Implement Sorted Search and Array Updates - Bytedance (medium)
  • Find Maximum Candies With Two Types - Bytedance (medium)
  • Place Non-Attacking Queens - Bytedance (hard)
  • Compute Minimum Parentheses Additions - Bytedance (medium)