← Questions

Shuffle an array with a random-integer function

0
GoogleCodingLast asked Dec 2025

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 — permutes nums in place, calling rand_int exactly n - 1 times.
  • For testing, the calls go from the back: the first call is rand_int(0, n - 1), the next rand_int(0, n - 2), and so on down to rand_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_int may be used for randomness.

Clarifying questions

  • Is rand_int inclusive 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 i from n - 1 down to 1, pick j = rand_int(0, i) and swap nums[i] and nums[j]. O(n) time, O(1) space.
  • The proof: position n - 1 gets each card with probability 1/n, then the rest is a uniform shuffle of n - 1 cards. Equivalently the algorithm has exactly n · (n-1) · … · 2 = n! equally likely outcomes, each giving a different permutation.
  • Why "swap each i with rand_int(0, n - 1)" is biased: it has n^n equally likely outcomes, and n^n is not divisible by n! for n >= 3, so some orders must come up more often. Show it for n = 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. Build rand_int(0, k) without bias (rejection sampling).
  • Shuffle a stream whose length you don't know, or pick k random 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding