14. Add Two Numbers
mediumAsked at CoupangAdd two numbers stored as reverse-order linked lists, mirroring how Coupang's order-totalization pipeline streams partial sums across digit-precision boundaries during peak-event throughput.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given two non-empty linked lists representing two non-negative integers stored in reverse order. Each node contains a single digit. Add the two numbers and return the sum as a linked list.
Constraints
1 <= list length <= 1000 <= node value <= 9no leading zeros except the number 0
Examples
Example 1
l1=[2,4,3], l2=[5,6,4][7,0,8]Example 2
l1=[9,9,9,9,9,9,9], l2=[9,9,9,9][8,9,9,9,0,0,0,1]Approaches
1. Convert and add
Parse both lists into BigInt, add, rebuild list.
- Time
- O(n)
- Space
- O(n)
let a = 0n, b = 0n, p = 1n;
for (let n = l1; n; n = n.next) { a += BigInt(n.val) * p; p *= 10n; }Tradeoff:
2. Digit-by-digit with carry
Walk both lists, summing digits with a carry, and build the result list as you go.
- Time
- O(max(m,n))
- Space
- O(max(m,n))
function addTwoNumbers(l1, l2) {
const dummy = { val: 0, next: null };
let cur = dummy, carry = 0;
while (l1 || l2 || carry) {
const a = l1 ? l1.val : 0;
const b = l2 ? l2.val : 0;
const sum = a + b + carry;
carry = Math.floor(sum / 10);
cur.next = { val: sum % 10, next: null };
cur = cur.next;
if (l1) l1 = l1.next;
if (l2) l2 = l2.next;
}
return dummy.next;
}Tradeoff:
Coupang-specific tips
Coupang's order-totalization streams partial sums across digit-precision boundaries during peak-event throughput; the carry-streaming pattern shows you understand bounded-memory arithmetic over BigInt blow-up.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Coupang interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →