FastPrepMinimum Replacements Required to Make a Matrix Balanced
Problems/Minimum Replacements Required to Make a Matrix Balanced

Minimum Replacements Required to Make a Matrix Balanced

Microsoft logoMicrosoft● HardFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

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
matrix
012340WR???1R???W

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

More Microsoft interview questions

  • Maximum Score with Prime JumpsOA · Seen Dec 2026
  • Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Authentication SystemOA · Seen Nov 2026
  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Maximum String OperationsOA · Seen Nov 2026
  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
  • Daily Temperature By TownTwo Sigma · OATwo Sigma interview questions
  • Calculate the Max ValueAmazon · OA
  • Server SelectionAirbnb · OAAirbnb interview questions
  • Lexicographically Smallest K-Step Return WalkGoogle · ONSITE INTERVIEWGoogle interview questions
  • Town Temperature Regression AnalysisCitadel · PHONE SCREENCitadel interview questions
  • Minimum Row Deletions to Prevent a Target ORInMobi · ONSITE INTERVIEWInMobi interview questions
  • Flipping MatrixZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
  • Balanced Human, Model and Task AssignmentOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Best Starting Cell for Restaurant DistanceDoorDash · PHONE SCREENDoorDash interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int minimumReplacementsRequired(char[][] matrix) {
      // write your code here
    }

}
Checking account…