Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: What is the number of inversions in the array [4, 3, 2, 1, 0]?
- A. 4
- B. 10
- C. 20
- D. 15
Correct answer: B. 10
Every pair of the strictly decreasing array is inverted: C(5,2) = 10. Adjacent pairs give 4, ordered pairs give 20, and C(6,2) = 15 uses the wrong length.
More Coding Interview Problems questions
- Problem: The string "cabbage" is sorted by letter using a stable sort on the letters only. What is the 4th character of the sorted string?
- Problem: Starting with 1, the operations are: multiply by 2, then add 3, applied three times in a row (x → 2x + 3). What is the result…
- Problem: Find the number of integers n in the range 2 to 100 for which n and n + 2 are both prime (twin primes, counted by the smaller…
- Problem: The string "abcabcabcabc" is split into consecutive chunks of length 5 from the left (the last chunk may be shorter). How many…
- Problem: A binary search tree is built from the values 15, 10, 20, 8, 12, 17, 25 inserted in that order. What is the number of leaves…
- Problem: A stack-based simulation begins with an empty stack. Operations in order: push 4, push 7, push 2, pop, push 9, pop, push 5. What…