Data Structures & AlgorithmsVery hardGATE CS · competitive programming

One Lomuto partition pass with the last element as pivot runs on [3, 8, 2, 5, 1, 4, 7, 6]. What is the array after this pass, before any recursive call?

  1. A. [3, 2, 5, 1, 4, 6, 7, 8]
  2. B. [3, 2, 5, 1, 4, 8, 7, 6]
  3. C. [3, 2, 5, 1, 4, 6, 8, 7]
  4. D. [1, 2, 3, 4, 5, 6, 7, 8]

Correct answer: A. [3, 2, 5, 1, 4, 6, 7, 8]

Each element smaller than the pivot 6 is swapped to the front; the pivot is then swapped into index 5. Skipping that final swap leaves 8 at index 5, which is the trap.

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

Exams that ask this topic