Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

What is the minimum number of swaps of two arbitrary elements needed to sort the array [3, 1, 4, 2] into increasing order?

  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: C. 3

Map each element to its sorted position; the permutation splits into cycles, and a cycle of length L needs L - 1 swaps. Counting inversions gives a different quantity, which is a trap for swap counts.

More Coding Interview Problems questions

Exams that ask this topic