Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Find the number of inversions of the sequence 2, 4, 1, 3, 5, 0 processed with merge sort (count each cross-pair during merging).

  1. A. 7
  2. B. 2
  3. C. 15
  4. D. 8

Correct answer: D. 8

Inversions: 2 > 1, 0; 4 > 1, 3, 0; 1 > 0; 3 > 0; 5 > 0 → 8, which merge sort counts by adding (mid − i) on each cross-pair. Counting pairs with a[i] < a[j] instead gives 15 − 8 = 7, counting only adjacent descents gives 2, and 15 counts all pairs.

More Coding Interview Problems questions

Exams that ask this topic