Why Fast Pointer Moves at EXACTLY 2x (Floyd's Cycle Finding)
🎯 The Question
"In Floyd's Tortoise and Hare cycle detection algorithm, the slow pointer moves 1 step per iteration (
slow = slow.next) while the fast pointer moves 2 steps (fast = fast.next.next). Why does the fast pointer move at EXACTLY 2x speed? What happens mathematically if the fast pointer moves at 3x or 4x speed?"
⚡ 30-Second Elevator Pitch
The entire mathematical proof of cycle detection relies on Relative Velocity:
- When slow moves at speed and fast moves at speed :
- Inside a cycle of length , let the initial distance between fast and slow be nodes.
- On every iteration, the gap between them closes by exactly 1 node:
- Because integers decrement by 1, the distance is mathematically guaranteed to hit 0. The fast pointer can never "jump over" the slow pointer.
Why Not 3x Speed? If fast moves at speed 3, relative speed is . On every iteration, distance shrinks by 2: . If the cycle length is even and initial distance is odd, the fast pointer can jump over the slow pointer forever, looping multiple times before colliding and needlessly increasing time complexity.
🧠 Under-the-Hood: Distance Closing Inside the Cycle
🔬 Mathematical Proof: Cycle Entry Point
Beyond detecting a cycle, moving at provides the elegant mathematical foundation for finding the Cycle Entry Node:
Let:
- = Distance from head to cycle start.
- = Cycle perimeter length.
- = Distance from cycle start to meeting point.
At collision:
- (where complete loops)
Since fast traveled exactly twice as far:
This means: If you place one pointer at the head and keep one pointer at the meeting point, and advance both at speed 1, they will traverse distance and collide at the exact cycle start node! This mathematical property fails if fast speed is anything other than .
📌 Comparison Matrix: Fast Pointer Speeds in Cycle Detection
| Fast Pointer Speed | Relative Velocity () | Distance Sequence Modulo | Guaranteed Collision in 1 Lap? | Finds Cycle Entry Node? |
|---|---|---|---|---|
| 2x (Optimal) | 1 node / step | ✅ Yes (Guaranteed) | ✅ Yes () | |
| 3x | 2 nodes / step | ❌ No (Can skip if is even) | ❌ Complex modular math | |
| 4x | 3 nodes / step | ❌ No (Can skip if ) | ❌ Fails |
💡 What Interviewers Ask Next (Follow-Up Traps)
-
"What is the maximum number of steps before slow and fast meet in a cycle of length ?"
- Answer: At most iterations after the slow pointer enters the cycle. Because distance shrinks by 1 node per step and initial distance cannot exceed , they are guaranteed to meet in steps, keeping runtime strictly with auxiliary memory.
-
"Can Floyd's Cycle algorithm be applied to arrays without pointers?"
- Answer: Yes! This is the standard solution for LeetCode 287 (Find the Duplicate Number) in an array of integers in range . By treating each array value as a pointer to the next index (
next = arr[curr]), duplicate numbers create a cycle that can be detected in time and space.
- Answer: Yes! This is the standard solution for LeetCode 287 (Find the Duplicate Number) in an array of integers in range . By treating each array value as a pointer to the next index (
Interview Answer: Fast moves at exactly 2x speed so the relative speed between pointers is . A relative speed of 1 guarantees that the gap between pointers shrinks by exactly one node every step, ensuring the fast pointer cannot jump over the slow pointer and enabling the classic mathematical proof to locate the cycle entrance.