Random Flip Matrix - Problem

You are given an m x n binary grid matrix with all values initially set to 0. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flip it to 1.

All indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned. Optimize your algorithm to minimize the number of calls to the built-in random function and optimize time and space complexity.

Implement the Solution class:

  • Solution(int m, int n) - Initializes the object with the size of the binary matrix m and n
  • int[] flip() - Returns a random index [i, j] where matrix[i][j] == 0 and flips it to 1
  • void reset() - Resets all values of the matrix to 0

Input & Output

Example 1 — Basic 2x3 Matrix
$ Input: m = 2, n = 3
Output: [0,1] (or any valid position)
💡 Note: Initialize 2×3 matrix with all 0s. First flip() call can return any position like [0,1], which gets flipped to 1. Matrix becomes [[0,1,0],[0,0,0]].
Example 2 — Small 1x2 Matrix
$ Input: m = 1, n = 2
Output: [0,0] or [0,1]
💡 Note: With only 2 positions available, first flip returns either [0,0] or [0,1] with equal probability. Second flip would return the remaining position.
Example 3 — Single Cell Matrix
$ Input: m = 1, n = 1
Output: [0,0]
💡 Note: Only one position [0,0] available. First flip() must return [0,0]. Any subsequent flip() calls would have no valid positions (in practice, problem guarantees valid calls).

Constraints

  • 1 ≤ m, n ≤ 104
  • At most 1000 calls will be made to flip and reset
  • It is guaranteed that there will be at least one free cell for each call to flip

Visualization

Tap to expand
Random Flip Matrix - Hash Map Approach INPUT 2 x 3 Binary Matrix (all 0s) 0 0 0 0 0 0 j=0 j=1 j=2 i=0 i=1 Linear Index Mapping 0 1 2 3 4 5 Input Parameters m = 2 (rows) n = 3 (columns) total = m * n = 6 ALGORITHM STEPS 1 Initialize Set remaining = 6 HashMap map = empty 2 Random Pick rand = random(0, remaining-1) Example: rand = 1 3 Swap Logic x = map.get(rand, rand) map[rand] = map.get(rem-1) remaining-- 4 Convert to 2D row = x / n = 1 / 3 = 0 col = x % n = 1 % 3 = 1 Return [0, 1] HashMap State After Flip map[1] = 5 (swapped) remaining = 5 FINAL RESULT Matrix After Flip 0 1 0 0 0 0 Position [0,1] flipped! Output [0, 1] Complexity Analysis Time: O(1) per flip Space: O(flips) for HashMap Random calls: 1 per flip Key Insight: Use Fisher-Yates shuffle concept: map linear indices 0 to m*n-1 virtually. When picking index k, swap it with the last available index using a HashMap. This avoids storing the entire matrix while ensuring uniform random distribution. Only store swapped mappings - O(flips) space instead of O(m*n). TutorialsPoint - Random Flip Matrix | Hash Map Approach
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
32.4K Views
Medium Frequency
~25 min Avg. Time
856 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