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, 23, 12, 58, 19, 49, 6, 3, 2, 30] have?
- A. 15
- B. 30
- C. 31
- D. 29
Correct answer: B. 30
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 30 inversions, matching the O(n²) brute-force pair count.
More Coding Interview Problems questions
- Problem: 6 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- What is the length of the longest common subsequence of "AABEDAA" and "DEDAEECD"?
- 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…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Fractional knapsack, capacity 14 (items can be split). Items (weight, value): (8, 3), (6, 4), (4, 7), (3, 10). Using the greedy…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…