2. Add Two Numbers
mediumAsked at TwilioAdd Two Numbers gives Twilio interviewers a clean read on your linked-list mechanics: two non-empty lists hold digits in reverse, and you must return the digit-wise sum as a new linked list. The grading rubric weighs carry propagation, unequal lengths, and a trailing carry past both lists.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Twilio loops.
- Glassdoor (2026-Q1)— Listed across Twilio backend-SWE on-site rounds when the panel wants to stress-test pointer mechanics.
- LeetCode Discuss (2025-12)— Multiple Twilio interview reports cite this exact problem in the first on-site coding round.
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. Materialize to integers (rejected for the constraint set)
Walk each list to reconstruct the integer, sum them, then walk the result back into a new list.
- Time
- O(n)
- Space
- O(n)
function addTwoNumbersBrute(l1, l2) {
const toInt = (node) => {
let n = 0n, place = 1n;
while (node) { n += BigInt(node.val) * place; place *= 10n; node = node.next; }
return n;
};
let sum = toInt(l1) + toInt(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: Works but Twilio will flag it. It only avoids overflow because we use BigInt — in a language without arbitrary-precision integers (Java, Go) the lists can hold a 100-digit number and integer reconstruction overflows. The optimal solution is digit-by-digit, which is what the problem is actually testing.
2. Digit-by-digit with carry (optimal)
Walk both lists in lockstep, summing val1 + val2 + carry per node, appending to a new list with a dummy head.
- 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 v1 = l1 ? l1.val : 0;
const v2 = l2 ? l2.val : 0;
const sum = v1 + v2 + carry;
carry = Math.floor(sum / 10);
tail.next = { val: sum % 10, next: null };
tail = tail.next;
if (l1) l1 = l1.next;
if (l2) l2 = l2.next;
}
return dummy.next;
}Tradeoff: Avoids overflow because we never reconstruct the full integer. The carry being a loop condition is the elegance — handles the trailing-carry case (999 + 1 = 1000) without a post-loop branch.
Twilio-specific tips
Twilio's bar on this question is the carry handling and the trailing-carry edge case. The dummy-head pattern is the expected idiom — candidates who try to special-case the first node typically lose 10 minutes on off-by-one bugs. Twilio panels often follow this with a linked-list reverse (LC 206) or a merge of two sorted lists (LC 21).
Common mistakes
- Forgetting the trailing-carry case — for example, [9,9,9] + [1] must produce [0,0,0,1], so the loop must continue while carry is non-zero even after both lists are exhausted.
- Special-casing the first node instead of using a dummy head, which leads to off-by-one errors and ugly branching.
- Mutating the input lists in-place when the problem says to return a new linked list.
Follow-up questions
An interviewer at Twilio may pivot to one of these next:
- What if the digits were stored in forward order instead of reverse? (Reverse both lists, run the same algorithm, reverse the result; or use two stacks.)
- What if the linked lists could each be 10^7 long? (Streaming sum with carry is the only viable approach — int reconstruction is impossible.)
- How would you adapt this to subtract instead of add, with possible negative results?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the dummy head pattern preferred?
It eliminates a special case for the first node. With a dummy, the tail pointer always has a valid prev to attach to; without one, you have to branch on 'is this the first node?' every iteration, which is bug-prone under interview pressure.
Why does Twilio specifically like this problem?
It maps cleanly to the streaming-aggregation patterns Twilio uses in production (carry-style state propagation across message segments). The on-site interview rubric explicitly looks for clean handling of unequal-length inputs and trailing state, which mirrors what backend services do when summing across paginated APIs.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Twilio interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →