DSA interview question
A calculator for arbitrarily large integers stores each number as a linked list of digits in reverse order, least significant digit first, so 2 -> 4 -> 3 is 342. Adding two such numbers is schoolbook addition, and the reverse order is exactly what makes it easy: walk both lists together from the head, add digit by digit with a carry, and build the result list as you go. The only details are lists of different lengths and a final carry that adds one extra digit.
Example: 2 -> 4 -> 3 (342) + 5 -> 6 -> 4 (465) → 7 -> 0 -> 8 (807).
Node* addTwoNumbers(Node* l1, Node* l2); // digits in reverse order; returns a new list
Input (l1, l2) | Output | Explanation |
|---|---|---|
(2 -> 4 -> 3, 5 -> 6 -> 4) | 7 -> 0 -> 8 | 342 + 465 = 807 |
(0, 0) | 0 | |
(9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9, 9 -> 9 -> 9 -> 9) | 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1 | Different lengths and a final carry |
(1, 9 -> 9) | 0 -> 0 -> 1 | 1 + 99 = 100 |
(5, 5) | 0 -> 1 | The final carry creates a new digit |
(Confirm: digits are stored least significant first; the inputs have no leading zeros except the number 0; the result is a new list.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in