Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

Array [1, 3, 2, 6, 4, 5, 7, 8]. How many inversions does it have (index pairs i < j with a[i] > a[j])?

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

Correct answer: D. 3

An inversion is a pair in the wrong relative order. Merge sort counts cross-half inversions during the merge, giving the same total as the quadratic count. Counting equal pairs with >= is a different quantity.

More Coding Interview Problems questions

Exams that ask this topic