Quant Memo
Coding/●●●●

Count out-of-order pairs in a price series

Asked at Two Sigma, Point72

An "inversion" is a pair of positions i < j where the earlier value is greater than the later one. The total inversion count measures how far a series is from sorted, a crude gauge of downward disorder. Count all inversions.

values = [2, 4, 1, 3, 5]
-> 3        # (2,1), (4,1), (4,3)

Return the number of inversions, in O(n log n) time. The brute-force pairwise count is O(n^2).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions