Data Structures & AlgorithmsHard

Find the duplicate in 2 5 9 6 9 3 8 9 7 1 (values 1 to 9, one repeated) with Floyd's method: slow = a[slow] and fast = a[a[fast]], both starting at a[0]. Count the total number of loop iterations across both phases: phase 1 runs until the two pointers meet, and phase 2 runs until the two pointers meet again at the duplicate.

  1. A. 7
  2. B. 1
  3. C. 14
  4. D. 8

Correct answer: D. 8

Phase 1 (slow one step, fast two) meets after 7 iterations; phase 2 restarts slow at a[0] and the pointers meet at the duplicate 9 after 1 more iteration, total 8. Counting only phase 1 gives 7, only phase 2 gives 1, and 14 counts the fast pointer's moves in phase 1.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic