Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: Bubble sort is run on [5, 2, 9, 1, 7, 3, 8, 6, 4] using the standard adjacent-swap pass (for i from 0 to n−2, swap a[j] and a[j+1] when a[j] > a[j+1]). How many swaps are performed in total?
- A. 36
- B. 17
- C. 8
- D. 7
Correct answer: B. 17
Each adjacent swap removes exactly one inversion, so the swap count equals the 17 inversions of the input. Counting comparisons gives 9·8/2 = 36, counting the n − 1 = 8 passes gives 8, and selection sort would need only 7 swaps.
More Coding Interview Problems questions
- Problem: A list starts as [1, 2, 3, 4, 5, 6, 7, 8]. Four operations are applied in order: rotate the list left by 3; reverse the first 4…
- Problem: XOR of all integers from l = 13 to r = 46 inclusive. Compute the result.
- Problem: The number of trailing zeros of 500! written in base 6 (i.e., how many times 6 divides 500!) is what value?
- Problem: A 5×5 grid has 1s and 0s: 11000 / 11011 / 00100 / 01100 / 10001 (rows top to bottom). You may flip exactly one 0 into a 1. What…
- Problem: A DAG with vertices 0–5 has weighted edges 0→1 (2), 0→2 (1), 1→3 (2), 2→3 (3), 2→4 (1), 3→5 (1), 4→5 (5). How many distinct…
- Problem: An undirected connected graph has 9 vertices and 13 edges. What is the cyclomatic number (the number of independent cycles…