Data Structures & AlgorithmsHardTCS NQT · Infosys
Problem: The array [3, 1, 2] undergoes one pass of bubble sort (compare each adjacent pair from left to right and swap if out of order). How many inversions remain in the array afterwards?
- A. 1
- B. 0
- C. 2
- D. 3
Correct answer: B. 0
One bubble pass moves the largest element to the end and removes one inversion per swap; the remaining inversions are then counted on the new order. Counting the initial inversions gives a different number.
More Coding Interview Problems questions
- Problem: In the array [2, 9, 4, 7, 1, 8], find the 4th smallest element using the quickselect idea (its final answer is the value that…
- Problem: In the sorted array [1, 2, 4, 7, 8, 11, 13], how many pairs (i, j) with i < j have a[j] − a[i] ≤ 2?
- Problem: Piles of bananas are [3, 6, 7, 11] and you must finish all of them within 8 hours. Each hour you eat up to k bananas from one…
- Problem: Intervals [1,4], [3,8], [6,9], [10,12], [2,5] are given. How many of them contain the point 5 (endpoints included)?
- Problem: Compute (1³ + 2³ + … + 100³) modulo 1000.
- Problem: A trie stores the words "apple", "app", "apply", "ape", "banana". How many stored words start with the prefix "app" (including a…