DSA interview question
When the music player finishes a song, it must remove the head of the queue: the second song becomes the new head, and the old head's memory is freed. The same code base also removes a song at position k when the user swipes it away. Deleting the head is O(1): remember the old head, move head to head->next, free the old node. Deleting at position k needs the predecessor, so its next can skip over the removed node. As with insertion, the empty list and head deletion are the edge cases to get right.
Example: 1 -> 2 -> 3, delete the head → 2 -> 3.
Node* deleteHead(Node* head); // returns the new head (nullptr if the list becomes empty)
Node* deleteAtPosition(Node* head, int pos); // pos is 1-indexed and valid
| Input | Output list | Explanation |
|---|---|---|
deleteHead(1 -> 2 -> 3) | 2 -> 3 | |
deleteHead(5) | (empty) | The only node is removed |
deleteHead(empty) | (empty) | Nothing to delete |
deleteAtPosition(1 -> 2 -> 3 -> 4, 3) | 1 -> 2 -> 4 | The middle node is removed |
deleteAtPosition(1 -> 2, 1) | 2 | Position 1 is the head |
deleteAtPosition(1 -> 2 -> 3, 3) | 1 -> 2 | The tail |
(Confirm: positions are 1-indexed and valid; removed nodes are freed; the function returns the new head.)
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