Add Two Forward-Order Lists
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates linked list manipulation and numerical addition represented in forward-order digit encoding, testing competency in handling digit-by-digit arithmetic, carry propagation, and edge cases such as differing list lengths and final carry.
Constraints
- 0 <= len(l1), len(l2)
- Each digit is in the range 0-9.
- No leading zeros unless the number is exactly zero ([0]).
- The empty list [] is treated as the value 0.
Examples
Input: ([7, 2, 4, 3], [5, 6, 4])
Expected Output: [7, 8, 0, 7]
Explanation: 7243 + 564 = 7807.
Input: ([0], [0])
Expected Output: [0]
Explanation: 0 + 0 = 0; the result keeps a single zero digit.
Input: ([9, 9, 9], [1])
Expected Output: [1, 0, 0, 0]
Explanation: 999 + 1 = 1000; the carry propagates through every digit and adds a new most-significant digit.
Input: ([5], [5])
Expected Output: [1, 0]
Explanation: 5 + 5 = 10; single-digit inputs produce a carry.
Input: ([1, 2, 3], [])
Expected Output: [1, 2, 3]
Explanation: The empty list is 0, so 123 + 0 = 123.
Input: ([2, 4, 9, 5, 6], [7, 5, 3, 4])
Expected Output: [3, 2, 4, 9, 0]
Explanation: 24956 + 7534 = 32490; different lengths with multiple internal carries.
Input: ([0], [9, 9])
Expected Output: [9, 9]
Explanation: 0 + 99 = 99; a zero operand leaves the other number unchanged.
Hints
- Forward order means the head is the most significant digit, so naive node-by-node addition would add the wrong digits together — you must align the numbers by their least significant ends.
- One clean approach: convert each list to its integer value, add, then peel digits off the sum from least to most significant and reverse the result.
- To avoid reversing the inputs (the follow-up), push each list's digits onto a stack (or compare lengths and pad), then add from the back, carrying as you go, and prepend each result digit.