PPractick

DSA interview question

Find the Starting Point of a Loop in a Linked List

Medium · Data structures · Topics: linkedlist, two pointer

The problem

The scheduler bug from the previous problem needs fixing, not just detecting: the engineers want the exact node where the cycle begins, so they can cut the bad link. Floyd's algorithm has a remarkable second phase. After slow and fast meet inside the cycle, reset one pointer to the head and move both one step at a time; they meet again exactly at the cycle's entrance. A little algebra explains why, and interviewers usually ask for it.

Example: 3 -> 2 -> 0 -> -4, with -4 linking back to 2 → the node with value 2 (index 1).

Function signature

Node* detectCycleStart(Node* head);  // entrance node, or nullptr if there's no cycle

Test cases

Input (values, tail links to index)Output (index of the entrance)Explanation
[3, 2, 0, -4], pos 11Value 2
[1, 2], pos 00The whole list is a cycle
[1], pos -1-1No cycle (nullptr)
[1, 2, 3, 4, 5, 6], pos 22Value 3
[1], pos 00Self-loop

(Confirm: the output is reported as the entrance node's index for readability; the function itself returns the node or nullptr and must not modify the list.)

How interviewers phrase it

Clarifying questions to ask

  1. Return the node, or its index?
  2. Can I modify the list?
  3. Is O(1) extra space required?

Follow-ups to expect

  1. "Prove it."
  2. "Why do both pointers move one step in phase two?"
  3. "What's the hash-set version?"
  4. "How do you remove the cycle once you've found the entrance?"
  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