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 [26, 56, 48, 41, 18, 39, 5, 7, 4, 1] have?
- A. 39
- B. 38
- C. 40
- D. 6
Correct answer: A. 39
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 39 inversions, matching the O(n²) brute-force pair count.
More Coding Interview Problems questions
- Fractional knapsack, capacity 16 (items can be split). Items (weight, value): (3, 8), (7, 6), (3, 8), (6, 8). Using the greedy…
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (5, 14), (6, 9), (2, 9), (5, 13). Using the greedy…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (3, 12), (6, 9), (3, 13), (5, 9), (5, 6). Using the greedy…
- 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 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…
- Fractional knapsack, capacity 17 (items can be split). Items (weight, value): (4, 14), (7, 5), (5, 4), (5, 10). Using the greedy…