DSA interview question
A music player keeps the current queue of songs, and users constantly insert songs at the front ("play next") and remove songs from the middle. Arrays make front insertions and middle deletions O(n) because everything has to shift. A singly linked list stores each item in its own node holding a value and a pointer to the next node, so insertions and deletions are just pointer changes once you're at the right place. The trade-off is that there's no indexing: reaching the k-th song means walking k nodes from the head. Your first task is the foundation for every linked-list problem: build a list from an array, traverse it, and turn it back into an array.
Example: [1, 2, 3, 4] → 1 -> 2 -> 3 -> 4.
struct Node { int data; Node* next; };
Node* arrayToList(const vector<int>& a); // returns the head (nullptr for an empty array)
vector<int> listToArray(Node* head); // traverses from head to the end
Input a | List printed | Explanation |
|---|---|---|
[1, 2, 3, 4] | 1 -> 2 -> 3 -> 4 | Four nodes; the last points to nullptr |
[7] | 7 | The head is also the tail |
[] | (empty) | The head is nullptr |
[5, -1, 5] | 5 -> -1 -> 5 | Duplicates and negatives are fine |
(Confirm: listToArray(arrayToList(a)) must return a unchanged; all nodes are freed at the end.)
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