PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Shuffle array using random integer API

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Find Containing Range - Google (medium)
  • Rearrange Tasks With Cooldown - Google (medium)
  • Solve Three Array and Matrix Path Problems - Google (medium)
  • Consolidate On-Call Rotation Segments - Google (medium)
  • Solve Flower Placement and Directory Deletion - Google (medium)
Google logo
Google
Dec 8, 2025, 7:29 PM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

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:

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.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ 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.