Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: What is the number of inversions in the array [2, 5, 1, 4, 3]?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Inversions are pairs i < j with a[i] > a[j]: (2,1), (5,1), (5,4), (5,3), (4,3), totalling five.
More Coding Interview Problems questions
- Problem: The array [3, 7, 1, 9] is used to create all non-empty contiguous subarrays. How many of them have an odd sum?
- Problem: In the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], find the number of pairs (i, j) with i < j whose product is a multiple of 12.
- Problem: How many 5-digit numbers (10000–99999) have digits that sum to exactly 6 and begin with the digit 1?
- Problem: A string of 6 characters is formed from the letters {a, b} such that no two adjacent characters are equal. How many such strings…
- Problem: The Euclidean distance squared between (1,2) and (4,6) is what integer?
- Problem: In the sequence 2, 3, 5, 8, 12, 17, … each term increases by one more than the previous increase (differences 1, 2, 3, 4, …)…