Shuffle an array with a random-integer function
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Dec 2025. It tests randomization and arrays.
You have a deck of distinct cards in an array and no built-in shuffle. Your only source of randomness is rand_int(l, r), which returns a uniformly random integer in [l, r], inclusive, independently on every call. Shuffle the array in place so that every one of the n! orders is equally likely, in O(n) time and O(1) extra space. Then explain why it is uniform.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
shuffle(nums: list[int]) -> None— permutesnumsin place, callingrand_intexactlyn - 1times. - For testing, the calls go from the back: the first call is
rand_int(0, n - 1), the nextrand_int(0, n - 2), and so on down torand_int(0, 1). Each result is the index swapped into the last unshuffled position.
Example
nums = [1, 2, 3, 4], rand_int returns 1, 1, 0 in turn
i = 3: rand_int(0, 3) = 1, swap nums[3] and nums[1] -> [1, 4, 3, 2]
i = 2: rand_int(0, 2) = 1, swap nums[2] and nums[1] -> [1, 3, 4, 2]
i = 1: rand_int(0, 1) = 0, swap nums[1] and nums[0] -> [3, 1, 4, 2]
An empty or one-card deck needs no calls.
Constraints
-
0 <= n <= 10^5; values are distinct. - Only
rand_intmay be used for randomness.
Clarifying questions
- Is
rand_intinclusive on both ends, and truly uniform and independent? - In place, or may I return a new array?
- Should every permutation be equally likely, or just "random looking"?
- What should happen with an empty array?
What a strong answer covers
- Fisher-Yates: for
ifromn - 1down to 1, pickj = rand_int(0, i)and swapnums[i]andnums[j]. O(n) time, O(1) space. - The proof: position
n - 1gets each card with probability1/n, then the rest is a uniform shuffle ofn - 1cards. Equivalently the algorithm has exactlyn · (n-1) · … · 2 = n!equally likely outcomes, each giving a different permutation. - Why "swap each
iwithrand_int(0, n - 1)" is biased: it hasn^nequally likely outcomes, andn^nis not divisible byn!forn >= 3, so some orders must come up more often. Show it forn = 3(27 outcomes, 6 orders). - Off-by-one traps:
rand_int(0, i - 1)gives Sattolo's algorithm (only single cycles), not a uniform shuffle. - How to test: shuffle a small array many times and check each permutation's frequency with a chi-squared test.
Follow-ups
- You only have
rand_bit()returning 0 or 1. Buildrand_int(0, k)without bias (rejection sampling). - Shuffle a stream whose length you don't know, or pick
krandom cards from it (reservoir sampling). - Shuffle a deck too large for memory.
Where it's asked
- Google · First round: coding · Last asked Dec 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
randomization · arrays
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
