What is Floyd's cycle detection algorithm?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Data Structures & Algorithms topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

Floyd's cycle detection algorithm (Floyd's tortoise and hare) detects cycles in a linked list using two pointers moving at different speeds. The slow pointer advances one step at a time; the fast pointer advances two steps. If there is a cycle, the fast pointer eventually laps the slow pointer — they meet inside the cycle. If there is no cycle, the fast pointer reaches null. Cycle detection: slow = head; fast = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false;. Finding cycle start: once slow and fast meet, reset slow to head; advance both one step at a time — they meet at the cycle start. Finding cycle length: after detection, keep fast still, advance slow until it reaches fast again, counting steps. Time: O(n). Space: O(1). Applications: (1) Linked list cycle detection (LeetCode 141/142); (2) Finding duplicate in array of n+1 integers using array as implicit linked list; (3) Rho algorithm for integer factorization; (4) Detecting periodic sequences in hashing. Key insight: in a cycle of length L, the two pointers must meet within L steps of entering the cycle.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.