Prove an Incremental Shuffle Is Uniform
Prove an Incremental Shuffle Is Uniform
Consider the following in-place shuffle algorithm for an array of length n:
for i from 0 through n - 1:
j = a uniformly random integer in [0, i]
swap(array[i], array[j])
Explain what this function does and prove by induction that, assuming independent uniform random choices, every permutation of distinct input elements has a probability of exactly 1 / n!.
Constraints & Assumptions:
- The random range
[0, i]includes both endpoints. - Random choices at different iterations are independent and unbiased.
- Treat input elements as distinct for the proof.
- The implementation mutates the input array and uses constant auxiliary space.
AI interview prep
Core dilemma
Proving the uniformity of a randomized algorithm under strict mathematical conditions, balancing the intuitive understanding of shuffling with the rigor of inductive proof and the practical implications of random number generation.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Prove an Incremental Shuffle Is Uniform
Prove an Incremental Shuffle Is Uniform
Consider the following in-place shuffle algorithm for an array of length n:
for i from 0 through n - 1:
j = a uniformly random integer in [0, i]
swap(array[i], array[j])
Explain what this function does and prove by induction that, assuming independent uniform random choices, every permutation of distinct input elements has a probability of exactly 1 / n!.
Constraints & Assumptions:
- The random range
[0, i]includes both endpoints. - Random choices at different iterations are independent and unbiased.
- Treat input elements as distinct for the proof.
- The implementation mutates the input array and uses constant auxiliary space.