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 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.

Visualization

Tap to expand
๐ŸŽฏ Random Matrix Flip: Fisher-Yates Visualization3ร—2 Matrix โ†’ 1D Array Mapping[0,0][0,1][1,0][1,1][2,0][2,1]Becomes012345Array indices [0, 1, 2, 3, 4, 5]Step 1: First Flip - Random Index 3012345SelectedLastHashMap[3] = 5, total = 5, Return [1,0] (index 3 โ†’ row 1, col 0)Step 2: Second Flip - Random Index 1012ร—4ร—SelectedLastHashMap[1] = 4, total = 4, Return [0,1] (index 1 โ†’ row 0, col 1)๐ŸŽฏ Key Insight: Fisher-Yates Shuffle Magicโ€ข O(1) Time: Each flip uses one random call + constant hash operationsโ€ข O(k) Space: Hash map only stores k swapped positions out of mร—n totalโ€ข Perfect Distribution: Every remaining position has equal 1/(remaining) probabilityโ€ข No Gaps: Available positions always occupy indices [0, total-1] logically
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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each flip operation is constant time with hash map lookup and swap

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is number of flipped cells, storing only moved positions in hash map

n
2n
โœ“ Linear Space

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
Asked in
Google 42 Facebook 28 Amazon 31 Microsoft 19
78.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen