Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

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 [41, 34, 42, 38, 3, 11, 39, 13, 12, 26] have?

  1. A. 28
  2. B. 29
  3. C. 30
  4. D. 16

Correct answer: B. 29

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 29 inversions, matching the O(n²) brute-force pair count.

More Coding Interview Problems questions

Exams that ask this topic