Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: The number of inversions in a permutation of 1..8 given as [4, 7, 1, 8, 2, 6, 3, 5] is what value?

  1. A. 3
  2. B. 14
  3. C. 28
  4. D. 7

Correct answer: B. 14

Counting the smaller values to the right of each element: 3 + 5 + 0 + 4 + 0 + 2 + 0 + 0 = 14. Counting only adjacent descents gives 3, 28 counts all pairs, and counting elements not in their sorted position gives 7.

More Coding Interview Problems questions

Exams that ask this topic