DSA interview question
A bank's legacy system stores account numbers too long for any integer type as linked lists of digits, most significant digit first, like 4 -> 5 -> 6 for 456. The "next account number" feature must add 1. The hard part is the carry: it starts at the last digit but the list only points forward, and a run of 9s like 9 -> 9 -> 9 turns into 1 -> 0 -> 0 -> 0, which needs a brand-new head node. Recursion handles it naturally: recurse to the tail, add the 1 there, and pass the carry back up as the calls return.
Example: 4 -> 5 -> 6 → 4 -> 5 -> 7; 9 -> 9 -> 9 → 1 -> 0 -> 0 -> 0.
Node* addOne(Node* head); // digits, most significant first; returns the (possibly new) head
| Input list | Output list | Explanation |
|---|---|---|
4 -> 5 -> 6 | 4 -> 5 -> 7 | No carry |
1 -> 2 -> 9 | 1 -> 3 -> 0 | A carry into the tens digit |
9 -> 9 -> 9 | 1 -> 0 -> 0 -> 0 | A new head is needed |
0 | 1 | |
9 | 1 -> 0 |
(Confirm: the list is non-empty with digits 0–9, most significant first, and no leading zeros except the number 0 itself.)
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