Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' checks for not equal, which is incorrect here.
✗ Incorrect
We use '==' to check if the element equals zero.
2fill in blank
mediumComplete 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; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment errors.
Using '!=' or '<' will not correctly detect zeros.
✗ Incorrect
We check if the element equals zero to mark the row and column.
3fill in blank
hardFix 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; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' causes assignment instead of comparison.
Using '!=' or '>' will not correctly detect zero markers.
✗ Incorrect
We must use '==' to check if the column marker is zero.
4fill in blank
hardFill 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]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or -1 will not zero the elements.
Using null is invalid for int arrays.
✗ Incorrect
We set elements to zero to mark them as zeroed.
5fill in blank
hardFill all three blanks to complete the method signature and return type.
DSA Java
public [1] setZeroes([2] matrix) { // method body [3] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using int[][] as return type when method should be void.
Incorrect parameter type or missing return statement.
✗ Incorrect
The method returns nothing (void), takes int[][] matrix as parameter, and uses return to exit.
