Random Flip Matrix
A medium-tier problem at 44% community acceptance, tagged with Hash Table, Math, Reservoir Sampling. Reported in interviews at Goldman Sachs and 0 others.
Random Flip Matrix is a medium-difficulty problem that appears in Goldman Sachs interviews. You're building a data structure that simulates random flips in a matrix: reset it and flip cells until they're all on, tracking which cells are flipped without storing the entire matrix. The trap is thinking you need a 2D array. The acceptance rate sits at 43%, which means most candidates either miss the indexing trick or blow past it without understanding the constraint. If this lands in your live assessment and you blank on the coordinate-flip pattern, StealthCoder solves it in seconds while the proctor sees nothing.
Companies that ask "Random Flip Matrix"
Random Flip Matrix is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe trick is using a Hash Table to track only the flipped cells, combined with a math-based coordinate mapping to handle the random selection. When you pick a random cell, you map a 1D index to 2D coordinates using division and modulo. If that cell is already flipped, you swap it with the last unflipped cell and decrement your range. This keeps the time complexity constant and memory linear in the number of flipped cells, not the matrix size. Most candidates default to brute-force enumeration or think they need Reservoir Sampling up front. You don't. The Hash Table stores flipped states; the math handles the coordinate system. When you hit this under pressure in the OA, the indexing logic becomes the bottleneck. StealthCoder removes that bottleneck instantly.
Pattern tags
You know the problem.
Make sure you actually pass it.
Random Flip Matrix recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Random Flip Matrix interview FAQ
Is Random Flip Matrix actually asked at top companies?+
Yes. Goldman Sachs reports it in their interview loop. It appears less frequently than array or tree problems, but when it does land, candidates often misunderstand the indexing constraint. The 43% acceptance rate reflects that most people either skip it or solve it suboptimally.
What's the actual trick to avoid storing the full matrix?+
Use a Hash Table to store only flipped cells, not the entire grid. Map a random 1D index to 2D coordinates using integer division and modulo. If the cell is already flipped, swap it with an unflipped cell at the boundary and shrink your pool. This keeps memory proportional to flips, not matrix size.
Why does Reservoir Sampling appear in the topics if I don't need it?+
Reservoir Sampling is listed because some solutions frame the problem as sampling without replacement from an unseen population. The Hash Table approach accomplishes the same goal more directly. Both are valid frameworks; the math-based swap is more efficient and intuitive for this specific problem.
What do most candidates get wrong when they solve this?+
They either store the entire matrix in memory (defeats the purpose), or they fail to handle the coordinate mapping correctly when a cell is already flipped. The boundary swap logic is where off-by-one errors hide. Understanding why you swap with the last unflipped index is critical.
How does this relate to Hash Table and Math as topics?+
Hash Table stores state efficiently without full grid allocation. Math handles the coordinate system: converting between 1D random index and 2D cell position, then managing the shrinking pool as cells flip. Both are essential; neither alone is sufficient.
Want the actual problem statement? View "Random Flip Matrix" on LeetCode →