Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
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 [7, 24, 19, 45, 1, 55, 21, 14, 13] have?
- A. 18
- B. 19
- C. 20
- D. 17
Correct answer: B. 19
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 19 inversions, matching the O(n²) brute-force pair count.
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Problem: 7 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "ACEACCEBD" and "DBEADDEA"?
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…