← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Google SWE coding round, one question the whole time. They wanted me to implement Fisher-Yates from scratch using a custom random API and actually justify the uniformity, not just write the code.

Questions Asked (1)

Q1

Given an array of n distinct elements and a rand_int(l, r) API that returns a uniform random integer in [l, r], implement an in-place shuffle such that every permutation is equally likely. Describe the algorithm, prove it produces a uniform distribution, and give time and space complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew Fisher-Yates going in, so the code part was fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by describing the Fisher-Yates shuffle algorithm: iterate from the last index down to 1, and for each index i, swap the element at i with the element at a randomly chosen index j in [0, i]. Then prove uniformity by induction, showing that each element is equally likely to end up in each position, and state that the algorithm runs in O(n) time and O(1) extra space.

Pro tip: Mention that the naive approach of swapping each element with a random index in [0, n-1] produces a biased shuffle, and emphasize that the Fisher-Yates algorithm is optimal and widely used in practice (e.g., in Python's random.shuffle).

1. Clarify the problem and constraints

Confirm that the array has distinct elements, the shuffle must be in-place, and every permutation must be equally likely. Note that rand_int(l, r) is inclusive and uniform.

2. Describe the algorithm

Explain the Fisher-Yates shuffle: for i from n-1 down to 1, pick j = rand_int(0, i) and swap arr[i] and arr[j]. Alternatively, iterate forward from 0 to n-2, picking j in [i, n-1].

3. Prove uniformity

Use induction: after step i, the suffix arr[i..n-1] is a uniformly random permutation of the original elements that end up there. Show that each element has probability 1/n to be placed at each position.

4. Analyze complexity

Time complexity is O(n) because we perform n-1 swaps, each O(1). Space complexity is O(1) extra space since we shuffle in-place.

5. Discuss edge cases and alternatives

Mention handling of empty or single-element arrays, and contrast with the biased naive shuffle. Optionally, note that the algorithm can be adapted for other random APIs.

Key Points to Mention

  • Fisher-Yates shuffle algorithm (also known as Knuth shuffle)
  • In-place swapping to achieve O(1) space
  • Uniform distribution proof via induction
  • Time complexity O(n)
  • Avoiding the common biased shuffle mistake
  • Use of rand_int(0, i) to ensure each permutation has probability 1/n!

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.