Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current right-half element being merged in), how many inversions (pairs i < j with a[i] > a[j]) does [53, 3, 45, 48, 36, 9, 47, 32, 18, 51] have?

  1. A. 25
  2. B. 23
  3. C. 21
  4. D. 24

Correct answer: D. 24

Whenever an element from the right half is merged in before some remaining left-half elements, every one of those remaining left elements forms an inversion with it; summing these counts across the whole merge sort gives 24 inversions, matching the O(n²) brute-force pair count.

More Coding Interview Problems questions

Exams that ask this topic