Bird
Raised Fist0
DSA Javaprogramming~10 mins

Set Matrix Zeroes In Place in DSA Java - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the first row contains zero.

DSA Java
boolean firstRowHasZero = false;
for (int j = 0; j < matrix[0].length; j++) {
    if (matrix[0][j] [1] 0) {
        firstRowHasZero = true;
        break;
    }
}
Drag options to blanks, or click blank then click option'
A==
B=
C!=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' checks for not equal, which is incorrect here.
2fill in blank
medium

Complete the code to mark rows and columns that should be zeroed.

DSA Java
for (int i = 1; i < matrix.length; i++) {
    for (int j = 1; j < matrix[0].length; j++) {
        if (matrix[i][j] [1] 0) {
            matrix[i][0] = 0;
            matrix[0][j] = 0;
        }
    }
}
Drag options to blanks, or click blank then click option'
A=
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment errors.
Using '!=' or '<' will not correctly detect zeros.
3fill in blank
hard

Fix the error in the code that zeroes marked rows and columns.

DSA Java
for (int i = 1; i < matrix.length; i++) {
    for (int j = 1; j < matrix[0].length; j++) {
        if (matrix[i][0] == 0 || matrix[0][j] [1] 0) {
            matrix[i][j] = 0;
        }
    }
}
Drag options to blanks, or click blank then click option'
A>
B=
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' causes assignment instead of comparison.
Using '!=' or '>' will not correctly detect zero markers.
4fill in blank
hard

Fill both blanks to zero the first row and first column if needed.

DSA Java
if (firstRowHasZero) {
    for (int j = 0; j < matrix[0].length; j++) {
        matrix[0][j] = [1];
    }
}

if (firstColHasZero) {
    for (int i = 0; i < matrix.length; i++) {
        matrix[i][0] = [2];
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or -1 will not zero the elements.
Using null is invalid for int arrays.
5fill in blank
hard

Fill all three blanks to complete the method signature and return type.

DSA Java
public [1] setZeroes([2] matrix) {
    // method body
    [3]
}
Drag options to blanks, or click blank then click option'
Avoid
Bint[][]
Cint[][] matrix
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using int[][] as return type when method should be void.
Incorrect parameter type or missing return statement.

Practice

(1/5)
1. What is the main goal of the Set Matrix Zeroes In Place problem?
easy
A. Count the number of zero elements in the matrix.
B. Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space.
C. Sort each row of the matrix in ascending order.
D. Find the sum of all elements in the matrix.

Solution

  1. Step 1: Understand the problem requirement

    The problem asks to set entire rows and columns to zero if any element in them is zero, modifying the matrix directly.
  2. Step 2: Recognize the constraint of in-place modification

    We must do this without using extra space for another matrix or large data structures.
  3. Final Answer:

    Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. -> Option B
  4. Quick Check:

    Goal = Set rows and columns to zero in-place [OK]
Hint: Remember: zero means zero entire row and column in-place [OK]
Common Mistakes:
  • Using extra space like another matrix
  • Only zeroing rows or columns, not both
  • Not modifying the matrix in-place
2. Which of the following Java code snippets correctly declares a 2D integer matrix named matrix with 3 rows and 4 columns?
easy
A. int[][] matrix = new int[3][4];
B. int matrix[][] = new int[4][3];
C. int matrix[3][4];
D. int matrix = new int[3][4];

Solution

  1. Step 1: Recall Java 2D array declaration syntax

    In Java, 2D arrays are declared as int[][] name = new int[rows][columns];.
  2. Step 2: Match the correct dimensions

    int[][] matrix = new int[3][4]; uses 3 rows and 4 columns correctly. int matrix[][] = new int[4][3]; reverses dimensions, C is invalid syntax, D misses array brackets.
  3. Final Answer:

    int[][] matrix = new int[3][4]; -> Option A
  4. Quick Check:

    Java 2D array syntax = int[][] matrix = new int[3][4]; [OK]
Hint: Use int[][] for 2D arrays with new int[rows][cols] [OK]
Common Mistakes:
  • Swapping rows and columns in new int[][]
  • Using C-style array declaration
  • Missing brackets in declaration
3. What will be the state of the matrix after running the following Java code snippet for setting matrix zeroes in place?
int[][] matrix = {
  {1, 2, 0},
  {4, 5, 6},
  {7, 0, 9}
};

// After applying setZeroes(matrix) method
// (method sets entire row and column to zero if element is zero)
medium
A. [[0, 0, 0], [4, 0, 0], [0, 0, 0]]
B. [[1, 0, 0], [4, 0, 0], [0, 0, 0]]
C. [[0, 0, 0], [4, 5, 0], [0, 0, 0]]
D. [[0, 2, 0], [4, 0, 6], [0, 0, 0]]

Solution

  1. Step 1: Identify zero elements and their rows and columns

    Zeros are at positions (0,2) and (2,1). Rows 0 and 2, columns 1 and 2 must be zeroed.
  2. Step 2: Set entire rows and columns to zero

    Row 0 becomes [0,0,0], row 2 becomes [0,0,0]. Column 1 and 2 in row 1 become 0, so row 1 becomes [4,0,0].
  3. Final Answer:

    [[0, 0, 0], [4, 0, 0], [0, 0, 0]] -> Option A
  4. Quick Check:

    Zero rows and columns set correctly = [[0, 0, 0], [4, 0, 0], [0, 0, 0]] [OK]
Hint: Mark zero rows and columns first, then update matrix [OK]
Common Mistakes:
  • Only zeroing rows or columns, not both
  • Not updating all affected cells
  • Overwriting zeros too early
4. Consider this buggy Java code snippet for setting matrix zeroes in place:
public void setZeroes(int[][] matrix) {
  int rows = matrix.length;
  int cols = matrix[0].length;
  boolean[] rowZero = new boolean[rows];
  boolean[] colZero = new boolean[cols];

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      if (matrix[i][j] == 0) {
        rowZero[i] = true;
        colZero[j] = true;
      }
    }
  }

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      if (rowZero[i] || colZero[j]) {
        matrix[i][j] = 0;
      }
    }
  }
}
What is the main issue with this code regarding the problem constraints?
medium
A. It modifies the matrix before checking all zeros, causing incorrect results.
B. It does not correctly identify zero elements in the matrix.
C. It causes an ArrayIndexOutOfBoundsException.
D. It uses extra space proportional to rows and columns, violating in-place requirement.

Solution

  1. Step 1: Analyze space usage of the code

    The code uses two boolean arrays, rowZero and colZero, sized by rows and columns, which is extra space.
  2. Step 2: Compare with problem constraints

    The problem requires in-place modification without extra space, so using these arrays violates the constraint.
  3. Final Answer:

    It uses extra space proportional to rows and columns, violating in-place requirement. -> Option D
  4. Quick Check:

    Extra space used = violates in-place [OK]
Hint: Avoid extra arrays; use matrix first row/column as markers [OK]
Common Mistakes:
  • Ignoring space constraints
  • Using extra arrays for marking
  • Not handling first row/column carefully
5. You have a 4x4 matrix:
1 2 3 4
5 0 7 8
9 10 11 12
0 14 15 16
Using the in-place method for setting matrix zeroes, what will be the matrix after the operation?
hard
A. [[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 15, 16]]
B. [[0, 0, 0, 4], [0, 0, 0, 0], [0, 0, 0, 12], [0, 0, 0, 0]]
C. [[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]]
D. [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Solution

  1. Step 1: Identify zero positions and mark rows and columns

    Zeros at (1,1) and (3,0). Rows 1 and 3, columns 0 and 1 must be zeroed.
  2. Step 2: Set zeros for affected rows and columns

    Rows 1 and 3 become entirely zeros. Columns 0 and 1 become zeros across all rows, resulting in row 0: [0,0,3,4]; row 2: [0,0,11,12].
  3. Final Answer:

    [[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]] -> Option C
  4. Quick Check:

    Zero rows and columns updated correctly = [[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]] [OK]
Hint: Mark first row/column, then update matrix carefully [OK]
Common Mistakes:
  • Zeroing entire rows or columns unnecessarily
  • Missing zeros in some cells
  • Not handling first row and column markers properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard