DSA interview question
A music app has a "jump to the middle of the playlist" button. The playlist is a singly linked list whose length isn't stored. Counting the nodes and then walking half-way takes two passes. The tortoise and hare technique does it in one: a slow pointer moves one step at a time and a fast pointer moves two. When the fast pointer runs off the end, the slow one is exactly in the middle. For an even number of nodes, this version returns the second of the two middle nodes.
Example: 1 -> 2 -> 3 -> 4 -> 5 → node 3; 1 -> 2 -> 3 -> 4 -> 5 -> 6 → node 4.
Node* middleNode(Node* head); // second middle for even lengths; head is non-empty
| Input list | Output (value) | Explanation |
|---|---|---|
1 -> 2 -> 3 -> 4 -> 5 | 3 | Odd length: the exact middle |
1 -> 2 -> 3 -> 4 -> 5 -> 6 | 4 | Even length: the second middle |
1 | 1 | |
1 -> 2 | 2 |
(Confirm: the list is non-empty; for an even length, return the second middle node (LeetCode 876).)
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