Data Structures & AlgorithmsHardProduct Company SDE · CP practice
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 [33, 56, 45, 2, 14, 48, 47, 51, 41, 8] have?
- A. 24
- B. 25
- C. 23
- D. 21
Correct answer: A. 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
- What is the length of the longest common subsequence of "EBBDBAD" and "ACBCBECC"?
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- What is the length of the longest common subsequence of "BDCEADE" and "EEABCADA"?
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (2, 10), (6, 14), (4, 13), (2, 13), (8, 15). Using the…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- What is the length of the longest common subsequence of "ABCECBA" and "ECEAAADE"?