Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
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 [55, 10, 24, 12, 29, 3, 39, 54, 37] have?
- A. 21
- B. 14
- C. 15
- D. 16
Correct answer: C. 15
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 15 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…
- 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 11 (items can be split). Items (weight, value): (5, 15), (4, 10), (4, 14), (4, 7). Using the greedy…
- A divide-and-conquer routine satisfies T(n) = 2T(n/2) + n / log n. What is its running time (by the master theorem, extended for polylog…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (5, 7), (6, 11), (7, 7), (4, 15). Using the greedy…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (6, 10), (3, 8), (4, 3), (2, 4), (2, 6). Using the greedy…