Count inversions in a permutation
Company: Hudson
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of inversion counting in permutations, testing algorithmic problem-solving, complexity analysis, and data-structure reasoning about pairwise order relations.
Constraints
- All values are distinct.
- 0 <= len(a) <= 200000
Examples
Input: ([1, 2, 3, 4],)
Expected Output: 0
Explanation: Already sorted has no inversions.
Input: ([4, 3, 2, 1],)
Expected Output: 6
Explanation: Reverse sorted has every pair inverted.
Input: ([2, 4, 1, 3, 5],)
Expected Output: 3
Explanation: Mixed ordering.
Input: ([],)
Expected Output: 0
Explanation: Empty input.
Input: ([3],)
Expected Output: 0
Explanation: Single element.
Hints
- A merge-sort count adds the number of remaining left elements when taking from the right.