Data Structures & AlgorithmsHardTCS NQT · Infosys

Problem: The array [3, 1, 2] undergoes one pass of bubble sort (compare each adjacent pair from left to right and swap if out of order). How many inversions remain in the array afterwards?

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

Correct answer: B. 0

One bubble pass moves the largest element to the end and removes one inversion per swap; the remaining inversions are then counted on the new order. Counting the initial inversions gives a different number.

More Coding Interview Problems questions

Exams that ask this topic