PPractick

DSA interview question

Introduction to Singly Linked Lists

Easy · Data structures · Topics: linkedlist

The problem

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.

Function signature

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

Test cases

Input aList printedExplanation
[1, 2, 3, 4]1 -> 2 -> 3 -> 4Four nodes; the last points to nullptr
[7]7The head is also the tail
[](empty)The head is nullptr
[5, -1, 5]5 -> -1 -> 5Duplicates and negatives are fine

(Confirm: listToArray(arrayToList(a)) must return a unchanged; all nodes are freed at the end.)

How interviewers phrase it

Clarifying questions to ask

  1. Singly or doubly linked?
  2. Should I handle an empty input?
  3. Who owns the memory: should I free the nodes?

Follow-ups to expect

  1. "Array vs linked list?"
  2. "Why keep a tail pointer while building?"
  3. "What does the last node point to?"
  4. "How do you avoid memory leaks in C++?"
  5. "What's a dummy (sentinel) head?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in