FastPrepAdd Signed Character Arrays

Add Signed Character Arrays

ByteDance logoByteDance● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Two signed decimal integers are stored in character arrays left and right. Each array contains an optional leading + or -, followed by one or more decimal digits.

Return their exact sum as a normalized character array:

  • Do not use a fixed-width numeric conversion or arbitrary-precision integer library.
  • Remove leading zeros from the magnitude.
  • Use a leading - only when the result is negative.
  • Represent zero exactly as ['0'].

Function

addSignedNumbers(left: char[], right: char[]) → char[]

Examples

Example 1

left = ["-","1","2","3"]right = ["4","5"]return = ["-","7","8"]

The operands are -123 and 45, whose sum is -78.

Example 2

left = ["9","9","9"]right = ["1"]return = ["1","0","0","0"]

A carry propagates through all three digits, producing 1000.

Example 3

left = ["-","5","0"]right = ["+","5","0"]return = ["0"]

Equal magnitudes with opposite signs cancel, and zero has no sign.

Constraints

  • 1 <= left.length, right.length <= 200000
  • Each input has an optional leading sign followed by at least one decimal digit.
  • Each input is a valid integer encoding; leading zeros are allowed.
  • The total input size fits in memory.

More ByteDance problems

See ByteDance hiring insights
public char[] addSignedNumbers(char[] left, char[] right) {
  // write your code here
}
left["-","1","2","3"]
right["4","5"]
expected["-","7","8"]
Checking account…