31. Add Two Numbers
mediumAsked at SnowflakeAdd two non-negative integers stored as linked lists in reverse-digit order. Snowflake asks this to test carry propagation logic — the same arithmetic kernel that underlies their high-precision NUMBER type's addition.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2026-Q1)— Snowflake numerics-team uses this as canonical big-number arithmetic warm-up.
- LeetCode Discuss (2025-11)— Recurring at Snowflake SDE-I onsites.
Problem
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Constraints
The number of nodes in each linked list is in the range [1, 100].0 <= Node.val <= 9It is guaranteed that the list represents a number that does not have leading zeros.
Examples
Example 1
l1 = [2,4,3], l2 = [5,6,4][7,0,8]Explanation: 342 + 465 = 807.
Example 2
l1 = [0], l2 = [0][0]Example 3
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9][8,9,9,9,0,0,0,1]Approaches
1. Convert to numbers, add, rebuild
Walk both lists to build BigInts, add, walk back to a list.
- Time
- O(max(m, n))
- Space
- O(max(m, n))
function addTwoNumbers(l1, l2) {
function toBig(node) {
let n = 0n, place = 1n;
while (node) { n += BigInt(node.val) * place; place *= 10n; node = node.next; }
return n;
}
let sum = toBig(l1) + toBig(l2);
const dummy = { val: 0, next: null };
let tail = dummy;
if (sum === 0n) return { val: 0, next: null };
while (sum > 0n) {
tail.next = { val: Number(sum % 10n), next: null };
tail = tail.next;
sum /= 10n;
}
return dummy.next;
}Tradeoff: Cute thanks to BigInt, but misses the point of the problem — show carry propagation.
2. Walk both lists with carry (optimal)
Walk both lists in parallel. At each step: sum = l1.val + l2.val + carry; new digit = sum % 10; carry = sum / 10. Stop when both null and carry == 0.
- Time
- O(max(m, n))
- Space
- O(max(m, n))
function addTwoNumbers(l1, l2) {
const dummy = { val: 0, next: null };
let tail = dummy;
let carry = 0;
while (l1 || l2 || carry) {
const a = l1 ? l1.val : 0;
const b = l2 ? l2.val : 0;
const sum = a + b + carry;
tail.next = { val: sum % 10, next: null };
tail = tail.next;
carry = Math.floor(sum / 10);
if (l1) l1 = l1.next;
if (l2) l2 = l2.next;
}
return dummy.next;
}Tradeoff: Streams digit-by-digit, exactly how a fixed-point arithmetic kernel works. Handles unequal lengths and final carry uniformly.
Snowflake-specific tips
Snowflake interviewers want carry handled in the loop condition, not as a special case afterward. Bonus signal: connect to NUMBER(38,2) addition — Snowflake's fixed-point arithmetic uses digit-by-digit addition with carry on the underlying decimal limbs.
Common mistakes
- Forgetting the final carry — list [5] + [5] = [0, 1], not [0].
- Skipping the unequal-length case by stopping when one list ends.
- Trying parseInt on long lists — overflows beyond 15 digits with regular Number.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Add Two Numbers II (LC 445) — digits stored in forward order.
- Multiply Strings (LC 43).
- How does Snowflake's NUMBER(38) addition work?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why include carry in the loop condition?
Otherwise [9] + [9] = [8] — you miss the final carry. The condition (l1 || l2 || carry) unifies all three cases.
Why reverse order?
Reverse-order storage means digits are ordered LSB-first, which is the natural direction for carry propagation. Forward order requires a stack or recursion.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →