Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: The array [5, 1, 4, 2, 3] is sorted by repeatedly swapping adjacent elements that are out of order (bubble sort). How many passes over the array are needed until no swap happens in a pass (including the final pass with no swaps)?

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

Correct answer: D. 3

Bubble sort performs passes until one pass makes no swaps. Here two passes perform swaps and a third pass confirms the array is sorted, giving three passes.

More Coding Interview Problems questions

Exams that ask this topic