Quick Overview

Add two signed decimal integers supplied as strings without converting the complete inputs to built-in integer types. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Add Two Signed Decimal Character Arrays

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

# Add Two Signed Decimal Character Arrays Add two signed decimal integers supplied as strings without converting the complete inputs to built-in integer types. Each input has an optional leading `+` or `-` followed by digits. Return a normalized decimal string: no leading zeros, no leading plus sign, and exactly `0` for zero. ## Function Contract Implement `add_signed_decimals(a, b) -> str`. ## Constraints - 1 <= length of each input <= 200000. - After an optional sign, each input contains at least one digit. - Inputs may contain leading zeros. - The result may be longer than either input and must not use arbitrary-precision parsing as a shortcut. ## Examples ```text a = "-00125", b = "+75" output = "-50" ``` ```text a = "999", b = "1" output = "1000" ``` ```hint Test complete cancellation Include opposite-signed inputs with equal absolute values and verify the normalized zero representation. ``` ```hint Exercise digit boundaries Use cases with long runs of nines or zeros, different input lengths, and leading signs and zeros. ```

Quick Answer: Add two signed decimal integers supplied as strings without converting the complete inputs to built-in integer types. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Add signed decimal integers `a` and `b` without converting either complete input to a built-in integer type. Each has an optional leading plus or minus followed by digits. Return a normalized decimal string with no leading zeros, no leading plus, and exactly `0` for zero.

Constraints

  • 1 <= len(a), len(b) <= 200000.
  • After an optional leading plus or minus, each input contains at least one decimal digit.
  • Inputs may contain leading zeros; output is normalized and may be one digit longer than either input.
  • Do not parse the complete input using built-in or arbitrary-precision integer conversion.

Examples

Input: ('0', '0')

Expected Output: '0'

Explanation: Two normalized zeros add to zero.

Input: ('+000', '-0')

Expected Output: '0'

Explanation: Signed leading-zero inputs normalize to exactly zero.

Hints

  1. Test complete opposite-sign cancellation, signed zero, and leading zeros.
  2. Include long runs of nines and zeros to exercise carry and borrow boundaries.
  3. Use both same-sign and opposite-sign operands with different normalized lengths.

Loading coding console...