Data Structures & AlgorithmsHardProduct-company MCQ pattern

A linked list has 3 nodes before the loop starts and a loop of 5 nodes. Floyd's slow pointer moves 1 step and fast pointer 2 steps per move, both starting at the head. After how many moves do they first meet?

  1. A. 5
  2. B. 8
  3. C. 15
  4. D. 3

Correct answer: A. 5

After k moves slow is k steps in and fast 2k; they meet when k ≥ 3 (slow is in the loop) and 2k − k = k is a multiple of 5, so k = 5. 8 = 3 + 5 is the total number of nodes, not the meeting time.

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

Exams that ask this topic