DSA interview question
A sensor log keeps its readings in a doubly linked list sorted by value, and repeated readings are noise. The cleanup job must keep one node per distinct value and free the rest, keeping both directions of the list consistent. Because the list is sorted, all copies of a value sit next to each other, so a single forward pass that compares each node with its successor is enough.
Example: 1 <-> 1 <-> 1 <-> 2 <-> 3 <-> 4 → 1 <-> 2 <-> 3 <-> 4.
DNode* removeDuplicates(DNode* head); // keeps the first node of each value; returns the head
| Input list | Output list | Explanation |
|---|---|---|
1 <-> 1 <-> 1 <-> 2 <-> 3 <-> 4 | 1 <-> 2 <-> 3 <-> 4 | A run at the head |
1 <-> 2 <-> 2 <-> 3 <-> 3 <-> 4 <-> 4 | 1 <-> 2 <-> 3 <-> 4 | Runs in the middle and at the tail |
5 <-> 5 <-> 5 | 5 | All equal |
1 <-> 2 <-> 3 | 1 <-> 2 <-> 3 | Nothing to remove |
(empty) | (empty) |
(Confirm: the list is sorted in non-decreasing order; removed nodes are freed; prev pointers must stay correct.)
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