Random Flip Matrix - Problem
Random Flip Matrix is a fascinating system design problem that combines randomization, space optimization, and efficient data structures.
You start with an
๐ฏ Randomly flip zeros to ones: Pick any cell containing 0, flip it to 1, and return its coordinates [i, j]. Every zero cell must have an equal probability of being selected.
๐ Reset the matrix: Set all cells back to 0, making them available for flipping again.
The Challenge: Optimize for both time and space complexity while minimizing calls to the random number generator. As cells get flipped, the available positions shrink - how do you maintain uniform randomness efficiently?
Example: With a 3ร2 matrix, initially all 6 positions are available. After flipping [1,0], only 5 positions remain, and each should have a 1/5 probability of being chosen next.
You start with an
m ร n binary matrix filled entirely with zeros. Your task is to design a smart data structure that can:๐ฏ Randomly flip zeros to ones: Pick any cell containing 0, flip it to 1, and return its coordinates [i, j]. Every zero cell must have an equal probability of being selected.
๐ Reset the matrix: Set all cells back to 0, making them available for flipping again.
The Challenge: Optimize for both time and space complexity while minimizing calls to the random number generator. As cells get flipped, the available positions shrink - how do you maintain uniform randomness efficiently?
Example: With a 3ร2 matrix, initially all 6 positions are available. After flipping [1,0], only 5 positions remain, and each should have a 1/5 probability of being chosen next.
Input & Output
example_1.py โ Basic Usage
$
Input:
solution = Solution(3, 1)
solution.flip() # return [1, 0]
solution.flip() # return [0, 0] or [2, 0]
solution.flip() # return remaining cell
solution.reset() # all cells become 0 again
โบ
Output:
[[1, 0], [0, 0], [2, 0]]
[reset completed]
๐ก Note:
A 3ร1 matrix has cells [0,0], [1,0], [2,0]. Each flip returns one of the remaining available cells with equal probability. After reset, all cells become available again.
example_2.py โ Small Matrix
$
Input:
solution = Solution(2, 2)
solution.flip() # return [0, 1] (example)
solution.flip() # return [1, 0] (example)
solution.reset()
solution.flip() # could return any cell again
โบ
Output:
[[0, 1], [1, 0]]
[reset completed]
[[1, 1]]
๐ก Note:
In a 2ร2 matrix, there are 4 possible positions. Each flip randomly selects from remaining available positions. Reset makes all positions available again.
example_3.py โ Edge Case
$
Input:
solution = Solution(1, 2)
solution.flip() # return [0, 0] or [0, 1]
solution.flip() # return the other cell
solution.reset() # reset to all zeros
โบ
Output:
[[0, 0], [0, 1]]
[reset completed]
๐ก Note:
Minimal case with just 2 cells. First flip has 50% chance for each cell, second flip returns the remaining cell with 100% probability.
Constraints
- 1 โค m, n โค 104
- There will be at least one cell with value 0 when flip is called
- At most 1000 calls will be made to flip and reset combined
Visualization
Tap to expand
Understanding the Visualization
1
Matrix as Array
Treat the 2D matrix as a 1D array of indices [0, 1, 2, ..., m*n-1]
2
Random Selection
Pick a random index from [0, total-1] where total is count of available positions
3
Smart Swap
Swap selected position with last available position using hash map
4
Shrink Range
Decrease total count, effectively removing last position from consideration
Key Takeaway
๐ฏ Key Insight: By treating the matrix as a 1D array and using Fisher-Yates shuffle with hash map optimization, we achieve O(1) time per flip while maintaining perfect randomness and using minimal space.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code