Data Structures & AlgorithmsModerateProduct Company SDE · merge-sort counting round
Problem: Count the inversions of the array [3, 1, 4, 1, 5, 9, 2, 6], where an inversion is a pair of positions i < j with a[i] > a[j] (equal values do not count).
- A. 9
- B. 8
- C. 3
- D. 19
Correct answer: B. 8
Pairs with a larger value first: 3 → (1, 1, 2), 4 → (1, 2), 5 → (2), 9 → (2, 6), totalling 8. Counting the equal pair (1, 1) gives 9, counting only adjacent descents (one bubble pass) gives 3, and counting a[i] < a[j] instead gives 19.
More Coding Interview Problems questions
- Problem: Huffman coding is applied to symbol frequencies 5, 9, 12, 13, 16, 45, always merging the two smallest weights. What is the total…
- Problem: Find the maximum of every window of size 3 in the array below, printing the list of window maxima. python from collections import…
- Problem: Compute the Z-array (z[i] = length of the longest common prefix of s and s[i:], with z[0] set to 0) for s = 'aabxaab'. python def…
- Problem: Count the palindromic substrings of 'abacaba' (every occurrence counts, so single characters count too). python def count(s)…
- Problem: A directed graph with V vertices is relaxed with Bellman-Ford. After V - 1 full rounds of relaxing all edges, one more round…
- Problem: A Fenwick tree of size 8 receives point adds (1,+5), (3,+2), (5,+7), (8,+1) with the usual add and prefix-query code, then prints…