PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of randomized algorithms, uniform probability distributions over permutations, in-place array manipulation, and time/space complexity analysis within the Coding & Algorithms domain.

  • hard
  • Google
  • Coding & Algorithms
  • Software Engineer

Shuffle array using random integer API

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given an array `nums` of length `n` representing a deck of distinct cards. You **do not** have access to a built-in shuffle function, but you are given the following API: ```python rand_int(l, r) # returns a uniformly random integer in [l, r], inclusive ``` ### Task Implement a function `shuffle(nums)` that randomly permutes the array **in place** such that **every one of the `n!` permutations is equally likely**. ### Requirements - You may only use `rand_int(l, r)` as your source of randomness. - The algorithm should run in `O(n)` time and `O(1)` extra space. - You may assume `rand_int` itself is perfectly uniform and independent between calls. Describe your algorithm, argue why the resulting permutation is uniform, and give the time and space complexity.

Quick Answer: This question evaluates understanding of randomized algorithms, uniform probability distributions over permutations, in-place array manipulation, and time/space complexity analysis within the Coding & Algorithms domain.

You are given an array `nums` of length `n` representing a deck of distinct cards. You do not have access to a built-in shuffle function. In the real interview setting, your only source of randomness is `rand_int(l, r)`, which returns a uniformly random integer in the inclusive range `[l, r]`. Implement the shuffle logic using the Fisher-Yates approach: randomly permute `nums` in place so that every one of the `n!` possible permutations is equally likely. For deterministic unit testing, this problem provides a second argument, `random_indices`, which simulates the outputs of `rand_int`. The first value in `random_indices` should be used as the result of `rand_int(0, n - 1)`, the second as the result of `rand_int(0, n - 2)`, and so on until `rand_int(0, 1)`. Your function should mutate `nums` in place and return it.

Constraints

  • 0 <= len(nums) <= 100000
  • All values in nums are distinct integers
  • len(random_indices) == max(0, len(nums) - 1)
  • For each k, 0 <= random_indices[k] <= len(nums) - 1 - k
  • The algorithm must run in O(n) time and use O(1) extra space, not counting the input arrays

Examples

Input: ([1, 2, 3, 4], [1, 1, 0])

Expected Output: [3, 1, 4, 2]

Explanation: Use indices 1, 1, and 0 for ranges [0,3], [0,2], and [0,1]. The swaps produce [1,4,3,2], then [1,3,4,2], then [3,1,4,2].

Input: ([], [])

Expected Output: []

Explanation: An empty deck has only one permutation, so no swaps are performed.

Input: ([42], [])

Expected Output: [42]

Explanation: A single-card deck is already shuffled; no random calls are needed.

Input: ([5, 6, 7], [2, 1])

Expected Output: [5, 6, 7]

Explanation: Each selected index is the same as the current position, so both swaps leave the array unchanged.

Input: ([-3, 0, 9, 2], [0, 0, 1])

Expected Output: [9, 0, 2, -3]

Explanation: Swap positions 3 and 0, then positions 2 and 0, then positions 1 and 1.

Input: ([10, 20], [0])

Expected Output: [20, 10]

Explanation: For two cards, selecting index 0 swaps the two elements.

Hints

  1. Think about filling the array from the end: which card should go into the last unshuffled position?
  2. After choosing a random index from the remaining prefix, a swap can place that chosen card without needing extra memory.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)