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?
- A. 3
- B. 14
- C. 28
- 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
- Problem: On a tree with 9 nodes labelled 1–9, edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8, 8-9, count the unordered pairs of nodes whose path…
- Problem: A string is a palindrome-partition target: what is the minimum number of cuts needed to split "abbacxcaxyyx" into palindromic…
- Problem: A word-ladder graph: vertices are words of length 3 from {cat, cot, cog, dot, dog, hat, hot} and two words are adjacent when they…
- Problem: Count the subarrays of [2, 1, 3, 2, 1, 4, 2] that have exactly 2 distinct values.
- Problem: Start at 1 and apply the rules: multiply by 3 if the number is even, add 5 if it is odd. Apply 9 steps. What is the final value?
- Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so…