384. Shuffle an Array
Asked at Uber
Difficulty
Medium
Time
O(n) per shuffle or reset
Space
O(n)
Pattern
Array
Companies
1
Problem
Design a class Solution that holds an array of integers and can do two things on demand: return the array in its original order, or return a randomly shuffled version of it.
The key rule is fairness: every possible ordering (permutation) of the array must have the same chance of appearing. A shuffle that favors some orderings is wrong, even if it looks random.
Implement three methods. The constructor Solution(nums) saves the starting array. reset() puts the array back to that starting order and returns it. shuffle() returns a random permutation of the array. Each call to shuffle() must be independent — calling it twice can give two different results.
Two operations on the same array
Constraints
- ●
1 <= nums.length <= 50 - ●
-10^6 <= nums[i] <= 10^6 - ●
All the elements of nums are unique. - ●
At most 10^4 calls in total will be made to reset and shuffle.
Examples
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []][null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]["Solution", "shuffle", "reset"]
[[[1]], [], []][null, [1], [1]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Uber | Medium | View all Uber questions → |
How to Think About It
Store the original array for reset operations
Implement Fisher-Yates shuffle for uniform random permutation
Iterate from last element to first, swapping with random index
Ensure random index is within valid range for each position
Reset simply restores the original array copy
Optimal Approach
Store a copy of the original array. For shuffle, iterate from the last element to the first. For each position i, generate a random index j where 0 <= j <= i, then swap elements at positions i and j. This Fisher-Yates algorithm guarantees uniform random permutation in O(n) time. Reset simply restores the saved original array.
Fisher-Yates: swap the last slot with a random earlier slot
What Trips People Up in Real Interviews
Clarify that shuffle must produce each permutation with equal probability
Explain Fisher-Yates gives O(n) time with uniform distribution
Discuss why copying the array for reset is necessary
Mention the importance of using inclusive random range correctly
Consider thread-safety concerns if asked about concurrent access
Solution Code
O(n) per shuffle or reset timeimport random
class Solution:
def __init__(self, nums):
self.nums = nums
self.original = nums[:]
def reset(self):
self.nums = self.original[:]
return self.nums
def shuffle(self):
arr = self.nums[:]
for i in range(len(arr) - 1, 0, -1):
j = random.randint(0, i)
arr[i], arr[j] = arr[j], arr[i]
self.nums = arr
return arrFrequently Asked Questions
What is the Shuffle an Array problem?
Design a class that shuffle an array of integers and returns it to its original configuration. The shuffle operation must generate all permutations with equal probability. Implement the shuffle using Fisher-Yates algorithm.
How do you solve Shuffle an Array?
The optimal approach runs in O(n) per shuffle or reset time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Shuffle an Array?
Shuffle an Array is asked at Uber. It is a medium difficulty problem.
What are common mistakes on Shuffle an Array?
- Clarify that shuffle must produce each permutation with equal probability
- Explain Fisher-Yates gives O(n) time with uniform distribution
- Discuss why copying the array for reset is necessary
- Mention the importance of using inclusive random range correctly
- Consider thread-safety concerns if asked about concurrent access