Data Structures & AlgorithmsHardProduct-company MCQ pattern

slow and fast both start at the head of 1 → 2 → 3 → 4 → 5 → 6. Loop A: while (fast && fast->next) { slow = slow->next; fast = fast->next->next; }. Loop B: the same but with condition (fast->next && fast->next->next). Where does slow stop in each case?

  1. A. A: 4, B: 4
  2. B. A: 4, B: 3
  3. C. A: 3, B: 3
  4. D. A: 3, B: 4

Correct answer: B. A: 4, B: 3

In A, fast goes 1 → 3 → 5 → past the end (null) while slow goes 1 → 2 → 3 → 4, the second middle. In B the loop stops when fast is at 5 (5.next.next is null), leaving slow at 3, the first middle. The difference matters for split-in-half algorithms.

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

Exams that ask this topic