Min Replacements Required to Make a Matrix Balanced
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The matrix in this Microsoft OA from December 2024 has exactly two rows, and that's the detail that makes it tractable. Every cell is R, W, or ?, and you need every row and every column to hold equal R and W counts, with ? ignored in the count. You want the fewest replacements. The hinted pattern is BFS, but with only two rows this reads more like a counting and greedy problem on columns. If you're taking it in the next day or two, know the column cases cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
You are given a matrix with exactly two rows and n columns. Every cell contains R, W, or ?. In one replacement, choose a cell containing ? and replace it with either R or W. Question marks that are not needed may remain unchanged. The matrix is balanced when every row and every column contains an equal number of R and W cells. Question marks are not counted. Return the minimum number of replacements required to make the matrix balanced. Function minimumReplacementsRequired(matrix: char[][]) → int Examples Example 1 matrix = [['W','R','?','?','?'],['R','?','?','?','W']] return = 4 Set the second cell of row 2 to W, the fifth cell of row 1 to R, and replace the two question marks in column 4 with W above R. The remaining ?/? column can stay unchanged. Each row then has two W and two R cells, and every column is balanced, for a total of 4 replacements. Constraints Standard Huge Constraints
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start with columns. A column with two ? is free and can stay. A column with R and W is already balanced. A column with R/R or W/W can never be balanced, since the only ways to change it is to replace a ?, and there are none. A column with one known cell and one ? must have that ? set to the opposite letter, so it's forced and costs one replacement. Those forced moves shift the row counts. Then check each row. If the row's balanced count needs more R or W, the ?/? columns can fix the gap, one letter in the top and the opposite in the bottom, which costs two replacements but changes each row by one. The pitfall is skipping the check that a row has an even number of letters and that the forced moves don't overshoot. Count forced cells, compute each row's deficit, then spend ?/? columns to close it. If you freeze on the case logic, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Min Replacements Required to Make a Matrix Balanced cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Min Replacements Required to Make a Matrix Balanced FAQ
What's the trick in the Min Replacements matrix problem?+
Work column by column, not cell by cell. With two rows, a column is either already balanced, impossible, forced to one replacement, or free if it's ?/?. Forced moves set the row totals, then free columns fix whatever imbalance remains.
Is BFS really needed here?+
The hint says breadth-first-search, but the two-row structure makes a direct counting pass enough. BFS over states would blow up on large inputs. Count column types, apply forced replacements, then settle row deficits. That's linear time.
When should the function return impossible or fail?+
The problem statement as given asks for a minimum count and doesn't define an impossible return. Still, watch for R/R or W/W columns, odd row lengths, and row deficits you can't cover. Check the statement in the assessment for the exact fallback value before you code it.
How hard is this Microsoft OA question really?+
Medium on paper, but the case analysis trips people up. The code is short once you see the column types. The risk is missing a case under time pressure, so write the four column cases down first, then code.
How do I prepare for this in 48 hours?+
Hand-trace Example 1 until the forced and free column logic feels automatic. Then write the solution from scratch once, with your own test cases for all-? columns, same-letter columns, and uneven rows. Don't spend time on BFS variants.