FastPrepAdd Large Numbers Stored as Lists

Add Large Numbers Stored as Lists

Google logoGoogle● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Two nonnegative integers are represented by arrays of decimal digits in most-significant-first order.

Add the two numbers and return their sum in the same representation. Each element is one digit from 0 through 9. The only representation with a leading zero is the one-digit number [0].

The inputs may be much longer than any built-in integer type, so perform the addition digit by digit.

Function

addLargeNumbers(a: int[], b: int[]) → int[]

Examples

Example 1

a = [9,9,9]b = [1]return = [1,0,0,0]

The carry propagates through all three digits.

Example 2

a = [1,2,3,4]b = [5,6,7]return = [1,8,0,1]

The represented values are 1234 and 567, whose sum is 1801.

Constraints

  • 1 <= a.length, b.length <= 100000.
  • Every element is an integer from 0 through 9.
  • Neither input has a leading zero unless its length is 1.

More Google problems

See Google hiring insights
public int[] addLargeNumbers(int[] a, int[] b) {
    // Write your code here.
}
a[9,9,9]
b[1]
expected[1,0,0,0]
Checking account…