Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Selection sort is run on [4, 3, 2, 1] in the usual way (for each position, find the minimum of the unsorted suffix and swap it into place). How many swap operations are performed, counting a swap even when the two indices are equal?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Selection sort performs one swap per position, whether or not the minimum is already in place. Counting only the swaps that move a value (skipping equal indices) gives a smaller number for sorted prefixes.
More Coding Interview Problems questions
- Problem: A Rule 90 cellular automaton on an infinite line starts with a single live cell. A cell's next state is 1 exactly when its two…
- Problem: A lucky 4-digit ticket has the sum of its first two digits equal to the sum of its last two digits. Each ticket number runs from…
- Problem: A grid of 5×5 cells has 'X' blocked cells at (0,2), (1,2), (2,2), (3,2) (row, column, 0-indexed); the rest are open. Cells…
- Problem: On a grid, a diagonal step costs 2 and a straight step costs 1 (moves are up, down, left, right, and the four diagonals). What is…
- Problem: The cycle graph C8 has vertices 0–7 with i adjacent to (i+1) mod 8. How many unordered pairs of vertices are at distance exactly…
- Problem: A height-balanced binary search tree is built over the sorted keys 1, 2, …, 10 by always taking the middle element as the root…