PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Modal

Prove an Incremental Shuffle Is Uniform

Last updated: Jul 14, 2026

Quick Overview

Prove that the forward Fisher-Yates shuffle produces every permutation of distinct elements with probability 1/n factorial. Use an induction invariant over the shuffled prefix and explain how endpoint mistakes or modulo-biased randomness break uniformity.

  • medium
  • Modal
  • Software Engineering Fundamentals
  • Software Engineer

Prove an Incremental Shuffle Is Uniform

Company: Modal

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

# Prove an Incremental Shuffle Is Uniform Consider this in-place shuffle for an array of length `n`: ```text for i from 0 through n - 1: j = a uniformly random integer in [0, i] swap(array[i], array[j]) ``` Explain what the function does and prove by induction that, assuming independent uniform random choices, every permutation of distinct input elements has probability exactly `1 / n!`. ### Constraints & Assumptions - The random range 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. ### Clarifying Questions to Ask - Is the random-number API's upper endpoint inclusive or exclusive? - Is the task asking for a proof of permutation uniformity or only uniform marginal positions? - May the input contain equal values, and if so are occurrences considered distinguishable? - Are cryptographic unpredictability and reproducibility relevant, or only mathematical fairness? ### What a Strong Answer Covers - Recognition as the forward form of the Fisher-Yates shuffle. - A precise induction invariant over the first `i + 1` positions. - Why each prior permutation and final location of the new element correspond to one outcome. - The probability calculation `1 / i! * 1 / (i + 1) = 1 / (i + 1)!`. - Random-range off-by-one errors and modulo-biased random-number generation. ### Follow-up Questions - Does proving every element has a uniform final position prove every permutation is uniform? - What changes when equal values are observationally indistinguishable? - How would a bad `random() % (i + 1)` implementation introduce bias? - How can you make a test reproducible without changing the production algorithm?

Quick Answer: Prove that the forward Fisher-Yates shuffle produces every permutation of distinct elements with probability 1/n factorial. Use an induction invariant over the shuffled prefix and explain how endpoint mistakes or modulo-biased randomness break uniformity.

Related Interview Questions

  • Explain and Harden Two Shell Commands - Modal (medium)
  • Sort One Hundred Million Digits - Modal (medium)
|Home/Software Engineering Fundamentals/Modal

Prove an Incremental Shuffle Is Uniform

Modal logo
Modal
Jul 12, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Prove an Incremental Shuffle Is Uniform

Consider this in-place shuffle 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 the function does and prove by induction that, assuming independent uniform random choices, every permutation of distinct input elements has probability exactly 1 / n!.

Constraints & Assumptions

  • The random range 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.

Clarifying Questions to Ask

  • Is the random-number API's upper endpoint inclusive or exclusive?
  • Is the task asking for a proof of permutation uniformity or only uniform marginal positions?
  • May the input contain equal values, and if so are occurrences considered distinguishable?
  • Are cryptographic unpredictability and reproducibility relevant, or only mathematical fairness?

What a Strong Answer Covers

  • Recognition as the forward form of the Fisher-Yates shuffle.
  • A precise induction invariant over the first i + 1 positions.
  • Why each prior permutation and final location of the new element correspond to one outcome.
  • The probability calculation 1 / i! * 1 / (i + 1) = 1 / (i + 1)! .
  • Random-range off-by-one errors and modulo-biased random-number generation.

Follow-up Questions

  • Does proving every element has a uniform final position prove every permutation is uniform?
  • What changes when equal values are observationally indistinguishable?
  • How would a bad random() % (i + 1) implementation introduce bias?
  • How can you make a test reproducible without changing the production algorithm?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Modal•More Software Engineer•Modal Software Engineer•Modal Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.