Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: The array [5, 1, 4, 2, 3] is sorted by repeatedly swapping adjacent elements that are out of order (bubble sort). How many passes over the array are needed until no swap happens in a pass (including the final pass with no swaps)?
- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
Bubble sort performs passes until one pass makes no swaps. Here two passes perform swaps and a third pass confirms the array is sorted, giving three passes.
More Coding Interview Problems questions
- Problem: The Hamming weight (number of 1-bits) of each integer from 0 to 15 is summed. What is the total?
- Problem: A set of letters {A, B, C, D, E} is arranged in a row. How many arrangements have A immediately to the left of B?
- Problem: The number of ways to split a line of 6 people into 2 groups of 3 (groups are distinguishable by their first member, so each…
- Problem: The string "racecar" is compared with its reverse position by position. How many positions have matching characters?
- Problem: Starting with the number 1, double it and add 1 repeatedly. What is the value after the 10th doubling step (starting value 1, 10…
- Problem: The array [2, 4, 3, 1, 6, 5] is partitioned into the largest possible number of consecutive chunks such that after sorting each…