Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
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 [1, 44, 12, 2, 51, 25, 15, 13, 43] have?
- A. 14
- B. 15
- C. 22
- D. 13
Correct answer: A. 14
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 14 inversions, matching the O(n²) brute-force pair count.
More Coding Interview Problems questions
- 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…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (3, 15), (7, 15), (4, 15), (5, 8), (3, 11). Using the greedy…
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…
- What is the length of the longest common subsequence of "DBDDDDEA" and "AADAADAB"?
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…