PPractick

DSA interview question

Middle of a Linked List (Tortoise and Hare)

Easy · Data structures · Topics: linkedlist, two pointer

The problem

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.

Function signature

Node* middleNode(Node* head);  // second middle for even lengths; head is non-empty

Test cases

Input listOutput (value)Explanation
1 -> 2 -> 3 -> 4 -> 53Odd length: the exact middle
1 -> 2 -> 3 -> 4 -> 5 -> 64Even length: the second middle
11
1 -> 22

(Confirm: the list is non-empty; for an even length, return the second middle node (LeetCode 876).)

How interviewers phrase it

Clarifying questions to ask

  1. For an even length, the first or the second middle?
  2. Can the list be empty?
  3. One pass required?

Follow-ups to expect

  1. "Why does the slow pointer end in the middle?"
  2. "How do you get the first middle for even lengths?"
  3. "What's the two-pass approach?"
  4. "Where else is the fast/slow idea used?"
  5. "Complexity?"

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