← Questions
Add two big integers stored as digit lists
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Nov 2025. It tests math and linked lists.
Two non-negative integers are too large for built-in number types, so each is stored as a list of digits (a linked list or an array), least significant digit first. Return their sum in the same form.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
add_numbers(a: list[int], b: list[int]) -> list[int]— both inputs non-empty, digits 0 to 9, least significant first, no leading zeros except the number 0 itself. The result follows the same rules.
Example
add_numbers([2, 4, 3], [5, 6, 4]) -> [7, 0, 8] # 342 + 465 = 807
add_numbers([9, 9, 9], [1]) -> [0, 0, 0, 1] # the carry adds a digit
add_numbers([0], [0]) -> [0]
Constraints
-
1 <= len(a), len(b) <= 10^5. - O(n + m) time and O(1) extra space beyond the output.
Clarifying questions
- Which end holds the least significant digit?
- Linked lists or arrays? May I modify the inputs?
- Can the numbers be negative?
- Is the output allowed to reuse input nodes?
What a strong answer covers
- Column addition: walk both lists together with one
carry; at each position add the available digits and the carry, emitsum % 10, keepsum // 10. - Keep going while either list has digits or the carry is non-zero, so different lengths and a final carry both work.
- O(max(n, m)) time, O(1) extra; a dummy head node keeps the linked-list version simple.
- The most-significant-first variant: reverse both lists, or use two stacks, or recurse after padding the shorter one; the trade-offs of each.
- Tests: different lengths, a carry that ripples through every digit, zero.
Follow-ups
- Digits are stored most significant first, and you may not reverse the inputs.
- Subtract or multiply the two numbers.
- Store several digits per node (base 10^9) to go faster. What changes?
Where it's asked
- Google · First round: coding · Last asked Nov 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
math · linked lists
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
