Fast and Slow Pointers: Mastering Floyd's Cycle Detection for Coding Interviews
7 views
Floyd's cycle detection, often called the "tortoise and hare" or fast and slow pointers technique, shows up in coding interviews far more often than its name suggests. It solves linked list cycle questions, but the same idea handles duplicate-number problems, "happy number" problems and finding the middle of a list, all in O(1) extra space. Interviewers like it because it separates candidates who memorized a trick from candidates who understand why it works.
THE BASIC IDEA
Move two pointers through a sequence. The slow pointer moves one step at a time; the fast pointer moves two. If the sequence ends (you hit null), there is no cycle. If there is a cycle, the fast pointer will eventually lap the slow one and they will meet inside the loop.
Why must they meet? Once both pointers are inside the cycle, the gap between them shrinks by exactly one each step. A gap that shrinks by one every step cannot skip past zero, so they collide.
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
return True
return False
FINDING WHERE THE CYCLE STARTS
The follow-up question is almost guaranteed: "Return the node where the cycle begins." After the pointers meet, reset one pointer to the head and move both one step at a time. The node where they meet again is the start of the cycle.
The short proof: let the distance from head to cycle start be a, from cycle start to meeting point b, and the cycle length c. When they meet, slow has walked a + b and fast has walked 2(a + b). The extra distance fast traveled is a whole number of loops, so a + b is a multiple of c. That means walking a more steps from the meeting point lands exactly on the cycle start, which is also a steps from the head.
Being able to explain this in three or four sentences is often worth more than the code.
OTHER PROBLEMS THAT ARE SECRETLY THE SAME
- Find the duplicate number in an array of n + 1 integers in the range 1..n, without modifying the array. Treat each value as a "next" pointer (i -> nums[i]). The duplicate creates a cycle, and its entry point is the answer.
- Happy number. Repeatedly replace a number with the sum of the squares of its digits. Either you reach 1 or you loop forever, and fast/slow pointers detect the loop without a hash set.
- Middle of a linked list. When fast reaches the end, slow is at the middle. This is also the first step of "reorder list" and "palindrome linked list."
MISTAKES TO AVOID
- Checking only fast.next and not fast first, which crashes on an empty list.
- Comparing values instead of node identity when nodes can share values.
- Starting fast one step ahead, then getting the cycle-start math wrong. Start both at head and keep the proof simple.
- Reaching for a hash set without mentioning the O(1)-space alternative. A set is fine as a first answer, but say that you can improve the space.
HOW TO PRESENT IT
Start with the brute-force idea (store visited nodes in a set, O(n) space), then offer fast and slow pointers as the optimization and explain why they must meet. Interviewers want to hear the reasoning out loud, and that is the part that tends to fall apart under pressure. Some candidates practice with tools like Linkjob AI (https://linkjob.ai), which gives real-time help during live job interviews and technical interviews, to keep their explanation structured when nerves hit. Either way, if you can draw the a, b, c diagram and walk through it calmly, this whole family of problems becomes easy points.