Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Array [1, 3, 2, 6, 4, 5, 7, 8]. How many inversions does it have (index pairs i < j with a[i] > a[j])?
- A. 4
- B. 1
- C. 5
- D. 3
Correct answer: D. 3
An inversion is a pair in the wrong relative order. Merge sort counts cross-half inversions during the merge, giving the same total as the quadratic count. Counting equal pairs with >= is a different quantity.
More Coding Interview Problems questions
- Sequence [5, 2, 9, 1, 7, 3]. Consider only index pairs i < j with j - i <= 2. What is the largest value of |a[i] - a[j]| over these pairs?
- What is the length of the longest strictly increasing subsequence of [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]? Use the patience-sorting method with…
- Sorted array [1, 2, 7, 8, 11, 15]. How many index triples i < j < k satisfy a[i] + a[j] + a[k] = 18?
- Four jobs have processing times [6, 3, 5, 2]. They run one at a time in some order, and job at position i (1-based) incurs cost i times…
- For the values [2, 5, 3, 8, 1, 6], how many values give remainder 1 or 2 when divided by 3 (counted once for each value)?
- Array [4, 4, 7, 1, 7, 2]. How many index pairs i < j have equal values a[i] = a[j]?