Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

A 'shuffle' swaps each position i with a position chosen uniformly from the WHOLE array (0..n−1), for i = 0..n−1. Why is this not a uniform shuffle?

  1. A. Because swapping an element with itself is wasted work
  2. B. Its nⁿ equally likely paths cannot split evenly among the n! permutations when n ≥ 3
  3. C. Because the last element is never moved
  4. D. It is uniform; it is merely slower than Fisher–Yates

Correct answer: B. Its nⁿ equally likely paths cannot split evenly among the n! permutations when n ≥ 3

Each of the nⁿ random choice sequences is equally likely and produces some permutation; a uniform result would need every permutation to get the same number of sequences, i.e. n! | nⁿ, which fails for n = 3 (27 vs 6). Fisher–Yates picks j from i..n−1, giving exactly n! paths.

More Coding Interview Problems questions

Exams that ask this topic