DSA interview question
The music player shows "12 songs in queue". The queue is a singly linked list and doesn't store its size, so the only way to know is to walk it from the head, counting nodes until you reach nullptr. It's the simplest linked-list traversal, and it's also the building block for many problems that first need the length: finding the middle, removing the n-th node from the end, rotating a list, or comparing two lists' lengths to find where they intersect.
Example: 1 -> 2 -> 3 -> 4 -> 5 → 5.
int length(Node* head); // iterative
int lengthRecursive(Node* head); // recursive
| Input list | Output | Explanation |
|---|---|---|
1 -> 2 -> 3 -> 4 -> 5 | 5 | |
(empty) | 0 | head is nullptr |
9 | 1 | |
4 -> 4 | 2 | Values don't matter, only nodes |
(Confirm: the list is acyclic; an empty list has length 0.)
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