DSA interview question
When the browser's history grows too long, it drops the oldest entry at the head. In a doubly linked list, deleting the head means moving head to the second node and clearing that node's prev, which still points at the node being freed. Leaving it would make any backward traversal read freed memory. The same care applies to deleting any node: both neighbours' pointers must be updated, which the prev pointer makes possible in O(1).
Example: 1 <-> 2 <-> 3, delete the head → 2 <-> 3.
DNode* deleteHead(DNode* head); // returns the new head
DNode* deleteAtPosition(DNode* head, int pos); // 1-indexed, valid position
| Input | Forward result | Explanation |
|---|---|---|
deleteHead(1 <-> 2 <-> 3) | 2 <-> 3 | The new head's prev is nullptr |
deleteHead(5) | (empty) | |
deleteHead(empty) | (empty) | |
deleteAtPosition(1 <-> 2 <-> 3 <-> 4, 3) | 1 <-> 2 <-> 4 | Both neighbours re-linked |
deleteAtPosition(1 <-> 2, 1) | 2 | Position 1 is the head |
deleteAtPosition(1 <-> 2 <-> 3, 3) | 1 <-> 2 | The tail |
(Confirm: after every deletion, the backward traversal must be the exact reverse of the forward one; deleted nodes are freed.)
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