Coding Interview Question: Zero Matrix

Zero Matrix: Coding Interview Question

Question

Given a boolean matrix, update it so that if any cell is true, all the cells in that row and column are true.

eg.

[true,  false, false]      [true,  true,  true ]
[false, false, false]  ->  [true,  false, false]
[false, false, false]      [true,  false, false]

Once you think that you’ve solved the problem, click below to see the solution.

 

As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.


Solution

How was that problem? You can check out the solution in the video below.

Here is the source code for the solution shown in the video (Github):

    
public void zeroMatrix(boolean[][] matrix) {
        
    // Verify the input array is nonzero
    if (matrix.length == 0 || matrix[0].length == 0) return;
        
    // Determine whether the first row or first column is true
    boolean rowZero = false, colZero = false;
    for (boolean i : matrix[0]) {
        rowZero |= i;
    }
    for (boolean[] i : matrix) {
        colZero |= i[0];
    }
            
    // For each cell not in the first row/column, if it is true, set the 
    // cell in the first row/same column and first column/same row to be 
    // true
    for (int i = 1; i < matrix.length; i++) {
        for (int j = 1; j < matrix[0].length; j++) {
            if (matrix[i][j]) {
                matrix[i][0] = true;
                matrix[0][j] = true;
            }
        }
    }
        
    // Go through the first column and set each row to true where cell in
    // the first column is true
    for (int i = 1; i < matrix.length; i++) {
        if (matrix[i][0]) {
            for (int j = 1; j < matrix[i].length; j++) {
                matrix[i][j] = true;
            }
        }
    }
        
    // Repeat for the rows
    for (int j = 1; j < matrix[0].length; j++) {
        if (matrix[0][j]) {
            for (int i = 1; i < matrix.length; i++) {
                matrix[i][j] = true;
            }
        }
    }
        
    // Set first row/column to true if necessary
    if (rowZero) {
        for (int i = 0; i < matrix[0].length; i++) {
            matrix[0][i] = true;
        }
    }
        
    if (colZero) {
        for (int i = 0; i < matrix.length; i++) {
            matrix[i][0] = true;
        }
    }
}

Did you get the right answer to this coding interview question? Please share your thoughts in the comments below.

Don't do another coding interview...​

…Until you’ve mastered these 50 questions!

50 Coding Interview Questions Cover
Sam Gavis-Hughson

Sam Gavis-Hughson

Sam, founder of Byte by Byte, helps software engineers successfully interview for jobs at top tech companies. Sam has helped thousands of students through his blog and free content -- as well as 400+ paying students -- land jobs at companies such as Google, Amazon, Microsoft, Bloomberg, Uber, and more.