Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: A linked list of 10 nodes is traversed with Floyd's tortoise and hare: the slow pointer moves 1 step and the fast pointer moves 2 steps each round. The list has a cycle that begins at node index 4 (0-indexed, the tail is nodes 0–3 and the cycle is nodes 4–9 and returns to node 4). After how many rounds do the two pointers first meet?

  1. A. 4
  2. B. 10
  3. C. 12
  4. D. 6

Correct answer: D. 6

Simulating: after 4 rounds slow enters the cycle at node 4 while fast is at node 8; the gap of 2 closes by 1 per round in a cycle of 6, so they meet after 6 rounds at node 6. 4 is only when slow enters the cycle, 10 is the list length, and 12 counts the fast pointer's steps.

More Coding Interview Problems questions

Exams that ask this topic