Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: Find the number of inversions of the sequence 2, 4, 1, 3, 5, 0 processed with merge sort (count each cross-pair during merging).
- A. 7
- B. 2
- C. 15
- D. 8
Correct answer: D. 8
Inversions: 2 > 1, 0; 4 > 1, 3, 0; 1 > 0; 3 > 0; 5 > 0 → 8, which merge sort counts by adding (mid − i) on each cross-pair. Counting pairs with a[i] < a[j] instead gives 15 − 8 = 7, counting only adjacent descents gives 2, and 15 counts all pairs.
More Coding Interview Problems questions
- Problem: A linked list is written as values 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8. The list is reversed in groups of 3 (the last group of fewer…
- Problem: The regex-like string match: count the substrings of "aabbaab" that match the pattern a+b (one or more a's immediately followed…
- Problem: Starting from the tree with root 1 and children 1: [2, 3], 2: [4], 3: [5, 6], 5: [7], a DFS visits children in increasing order…
- Problem: For each index of a = [4, 5, 2, 10, 8], find the index of the nearest element to its left that is strictly smaller (or -1). Run…
- Problem: A directed flow network has source s = 0 and sink t = 3 with arcs and capacities: 0→1 (3), 0→2 (2), 1→2 (1), 1→3 (2), 2→3 (3)…
- Problem: A bottleneck path from vertex 0 to vertex 4 minimises the largest edge weight used. Using the undirected edges 0-1 (7), 1-2 (3)…