← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Google SWE coding round with a shuffling algorithm question. Pretty standard stuff on the surface but the implementation details trip you up if you haven't thought about it carefully.

Questions Asked (1)

Q1

Given an integer array representing a deck of cards, implement the Fisher-Yates shuffle algorithm using only a basic random integer generator (no built-in shuffle functions allowed).

Algorithms & Data Structures
Author's notes

I knew the algorithm conceptually but fumbled the index range on the randint call.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and constraints, then explain the Fisher-Yates algorithm and its O(n) time and O(1) space complexity. Walk through the code step by step, emphasizing the loop from the end to the beginning and the random index selection. Finally, discuss correctness and potential pitfalls like off-by-one errors.

Pro tip: Mention that you can optimize by iterating from the end and swapping with a random index in [0, i], which avoids bias and is the modern version of the algorithm. Also, note that using a basic random integer generator means you need to ensure it produces uniformly distributed integers in the given range.

1. Clarify requirements and constraints

Confirm that the input is an integer array, the output should be a shuffled array (in-place or new), and that only a basic random integer generator is allowed. Ask about edge cases like empty array or single element.

2. Explain the Fisher-Yates algorithm

Describe the algorithm: iterate from the last element to the second element, pick a random index from 0 to the current index, and swap the current element with the randomly chosen one. Emphasize that this produces an unbiased permutation.

3. Implement the algorithm

Write code (or pseudocode) that uses a random integer generator to pick an index in the range [0, i]. Show the swap operation and ensure the loop bounds are correct.

4. Analyze complexity and correctness

State that time complexity is O(n) and space complexity is O(1) if shuffling in-place. Explain why each permutation is equally likely, and mention that the algorithm is unbiased.

5. Test with examples and edge cases

Walk through a small example (e.g., [1,2,3]) to demonstrate the steps. Discuss edge cases like empty array, single element, and potential issues with the random generator (e.g., modulo bias).

Key Points to Mention

  • Fisher-Yates shuffle produces an unbiased permutation, unlike naive approaches that can introduce bias.
  • The algorithm runs in O(n) time and O(1) extra space when shuffling in-place.
  • Iterating from the end and swapping with a random index in [0, i] ensures each element has an equal chance of ending up in any position.
  • Using a basic random integer generator requires careful handling of the range to avoid modulo bias.
  • Edge cases: empty array, single element, and arrays with duplicate values.
  • The algorithm can be implemented in-place, which is memory efficient.

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