Bird
Raised Fist0
DSA C#programming~20 mins

Set Matrix Zeroes In Place in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Zeroes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of the matrix after running the code?
Given the following C# code that sets matrix zeroes in place, what will be the final state of the matrix?
DSA C#
int[][] matrix = new int[][] {
    new int[] {1, 2, 3},
    new int[] {4, 0, 6},
    new int[] {7, 8, 9}
};

SetZeroes(matrix);

// Print matrix
for (int i = 0; i < matrix.Length; i++) {
    for (int j = 0; j < matrix[0].Length; j++) {
        Console.Write(matrix[i][j] + " ");
    }
    Console.WriteLine();
}

void SetZeroes(int[][] mat) {
    int m = mat.Length, n = mat[0].Length;
    bool firstRowZero = false, firstColZero = false;

    for (int i = 0; i < m; i++) {
        if (mat[i][0] == 0) firstColZero = true;
    }
    for (int j = 0; j < n; j++) {
        if (mat[0][j] == 0) firstRowZero = true;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][j] == 0) {
                mat[i][0] = 0;
                mat[0][j] = 0;
            }
        }
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][0] == 0 || mat[0][j] == 0) {
                mat[i][j] = 0;
            }
        }
    }

    if (firstColZero) {
        for (int i = 0; i < m; i++) mat[i][0] = 0;
    }
    if (firstRowZero) {
        for (int j = 0; j < n; j++) mat[0][j] = 0;
    }
}
A[[1, 0, 3], [4, 0, 6], [7, 0, 9]]
B[[1, 0, 3], [0, 0, 0], [0, 0, 0]]
C[[1, 2, 3], [0, 0, 0], [7, 8, 9]]
D[[1, 0, 3], [0, 0, 0], [7, 0, 9]]
Attempts:
2 left
💡 Hint
Check which rows and columns contain zero and how the first row and column are used as markers.
❓ Predict Output
intermediate
2:00remaining
What is the output of the matrix after running the code?
Given the following C# code that sets matrix zeroes in place, what will be the final state of the matrix?
DSA C#
int[][] matrix = new int[][] {
    new int[] {0, 1, 2, 0},
    new int[] {3, 4, 5, 2},
    new int[] {1, 3, 1, 5}
};

SetZeroes(matrix);

// Print matrix
for (int i = 0; i < matrix.Length; i++) {
    for (int j = 0; j < matrix[0].Length; j++) {
        Console.Write(matrix[i][j] + " ");
    }
    Console.WriteLine();
}

void SetZeroes(int[][] mat) {
    int m = mat.Length, n = mat[0].Length;
    bool firstRowZero = false, firstColZero = false;

    for (int i = 0; i < m; i++) {
        if (mat[i][0] == 0) firstColZero = true;
    }
    for (int j = 0; j < n; j++) {
        if (mat[0][j] == 0) firstRowZero = true;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][j] == 0) {
                mat[i][0] = 0;
                mat[0][j] = 0;
            }
        }
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][0] == 0 || mat[0][j] == 0) {
                mat[i][j] = 0;
            }
        }
    }

    if (firstColZero) {
        for (int i = 0; i < m; i++) mat[i][0] = 0;
    }
    if (firstRowZero) {
        for (int j = 0; j < n; j++) mat[0][j] = 0;
    }
}
A[[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
B[[0, 0, 0, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
C[[0, 1, 2, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
D[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Attempts:
2 left
💡 Hint
Remember the first row and column are used as markers and zeroed last if needed.
🔧 Debug
advanced
2:00remaining
What error occurs when running this code?
Consider the following C# code snippet that attempts to set matrix zeroes in place. What error will it raise when executed?
DSA C#
int[][] matrix = new int[][] {
    new int[] {1, 2},
    new int[] {3, 0},
    new int[] {5, 6}
};

SetZeroes(matrix);

void SetZeroes(int[][] mat) {
    int m = mat.Length, n = mat[0].Length;
    bool firstRowZero = false, firstColZero = false;

    for (int i = 0; i < m; i++) {
        if (mat[i][0] == 0) firstColZero = true;
    }

    for (int j = 0; j < n; j++) {
        if (mat[0][j] == 0) firstRowZero = true;
    }

    // rest of code omitted for brevity
}
ANullReferenceException
BIndexOutOfRangeException
CNo error, runs successfully
DInvalidOperationException
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
❓ Predict Output
advanced
2:00remaining
What is the output of the matrix after running the code?
Given the following C# code that sets matrix zeroes in place, what will be the final state of the matrix?
DSA C#
int[][] matrix = new int[][] {
    new int[] {1, 0, 3},
    new int[] {4, 5, 6},
    new int[] {7, 8, 0}
};

SetZeroes(matrix);

// Print matrix
for (int i = 0; i < matrix.Length; i++) {
    for (int j = 0; j < matrix[0].Length; j++) {
        Console.Write(matrix[i][j] + " ");
    }
    Console.WriteLine();
}

void SetZeroes(int[][] mat) {
    int m = mat.Length, n = mat[0].Length;
    bool firstRowZero = false, firstColZero = false;

    for (int i = 0; i < m; i++) {
        if (mat[i][0] == 0) firstColZero = true;
    }
    for (int j = 0; j < n; j++) {
        if (mat[0][j] == 0) firstRowZero = true;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][j] == 0) {
                mat[i][0] = 0;
                mat[0][j] = 0;
            }
        }
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][0] == 0 || mat[0][j] == 0) {
                mat[i][j] = 0;
            }
        }
    }

    if (firstColZero) {
        for (int i = 0; i < m; i++) mat[i][0] = 0;
    }
    if (firstRowZero) {
        for (int j = 0; j < n; j++) mat[0][j] = 0;
    }
}
A[[1, 0, 3], [4, 5, 6], [7, 8, 0]]
B[[1, 0, 3], [4, 0, 6], [7, 0, 0]]
C[[0, 0, 0], [4, 0, 6], [0, 0, 0]]
D[[0, 0, 0], [4, 5, 6], [0, 0, 0]]
Attempts:
2 left
💡 Hint
Remember to zero rows and columns marked by the first row and column, then handle first row and column last.
🚀 Application
expert
3:00remaining
How many zeroes are in the matrix after running the code?
Given the following C# code that sets matrix zeroes in place, how many zero elements will the matrix contain after execution?
DSA C#
int[][] matrix = new int[][] {
    new int[] {1, 2, 3, 4},
    new int[] {5, 0, 7, 8},
    new int[] {9, 10, 11, 12},
    new int[] {0, 14, 15, 16}
};

SetZeroes(matrix);

int zeroCount = 0;
for (int i = 0; i < matrix.Length; i++) {
    for (int j = 0; j < matrix[0].Length; j++) {
        if (matrix[i][j] == 0) zeroCount++;
    }
}

Console.WriteLine(zeroCount);

void SetZeroes(int[][] mat) {
    int m = mat.Length, n = mat[0].Length;
    bool firstRowZero = false, firstColZero = false;

    for (int i = 0; i < m; i++) {
        if (mat[i][0] == 0) firstColZero = true;
    }
    for (int j = 0; j < n; j++) {
        if (mat[0][j] == 0) firstRowZero = true;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][j] == 0) {
                mat[i][0] = 0;
                mat[0][j] = 0;
            }
        }
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (mat[i][0] == 0 || mat[0][j] == 0) {
                mat[i][j] = 0;
            }
        }
    }

    if (firstColZero) {
        for (int i = 0; i < m; i++) mat[i][0] = 0;
    }
    if (firstRowZero) {
        for (int j = 0; j < n; j++) mat[0][j] = 0;
    }
}
A9
B8
C10
D11
Attempts:
2 left
💡 Hint
Count zeroes after marking rows and columns, including first row and column if needed.

Practice

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

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 without extra space.
  2. Step 2: Differentiate from other operations

    Counting zeroes, replacing zeroes with ones, or sorting rows do not meet the problem's goal.
  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 C
  4. Quick Check:

    Goal is to set rows and columns zero in place = A [OK]
Hint: Focus on zero elements affecting rows and columns without extra space [OK]
Common Mistakes:
  • Thinking the problem is about counting zeroes
  • Trying to use extra memory like new arrays
  • Confusing zero replacement with sorting
2. Which of the following C# code snippets correctly declares a 2D integer matrix named matrix with 3 rows and 3 columns?
easy
A. int[,] matrix = new int[3,3];
B. int[][] matrix = new int[3][3];
C. int matrix[3,3];
D. int[] matrix = new int[3,3];

Solution

  1. Step 1: Recall C# 2D array syntax

    In C#, a rectangular 2D array is declared as int[,] matrix = new int[3,3];.
  2. Step 2: Identify incorrect syntax

    int[][] matrix = new int[3][3]; uses jagged array syntax incorrectly, C is invalid in C#, and D has type mismatch.
  3. Final Answer:

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

    Correct 2D array declaration = A [OK]
Hint: Use int[,] for 2D arrays, not jagged or invalid syntax [OK]
Common Mistakes:
  • Using jagged array syntax incorrectly
  • Missing array type in declaration
  • Using C/C++ style array declaration
3. What will be the state of the matrix after running the following C# code?
int[,] matrix = {
  {1, 2, 3},
  {4, 0, 6},
  {7, 8, 9}
};

// Set matrix zeroes in place
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);

bool firstRowZero = false;
bool firstColZero = false;

// Check first row
for (int j = 0; j < cols; j++) {
    if (matrix[0, j] == 0) firstRowZero = true;
}
// Check first column
for (int i = 0; i < rows; i++) {
    if (matrix[i, 0] == 0) firstColZero = true;
}

// Use first row and column as markers
for (int i = 1; i < rows; i++) {
    for (int j = 1; j < cols; j++) {
        if (matrix[i, j] == 0) {
            matrix[i, 0] = 0;
            matrix[0, j] = 0;
        }
    }
}

// Set zeroes based on markers
for (int i = 1; i < rows; i++) {
    for (int j = 1; j < cols; j++) {
        if (matrix[i, 0] == 0 || matrix[0, j] == 0) {
            matrix[i, j] = 0;
        }
    }
}

// Set first row zero if needed
if (firstRowZero) {
    for (int j = 0; j < cols; j++) matrix[0, j] = 0;
}
// Set first column zero if needed
if (firstColZero) {
    for (int i = 0; i < rows; i++) matrix[i, 0] = 0;
}
medium
A. [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
B. [[1, 0, 3], [0, 0, 0], [7, 0, 9]]
C. [[1, 0, 3], [0, 0, 0], [7, 0, 0]]
D. [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Solution

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

    Zero found at matrix[1,1]. Mark matrix[1,0] and matrix[0,1] as zero.
  2. Step 2: Set zeroes based on markers

    Set matrix[1,j] and matrix[i,1] to zero for i,j > 0. First row and column have no zero, so no change there.
  3. Final Answer:

    [[1, 0, 3], [0, 0, 0], [7, 0, 9]] -> Option B
  4. Quick Check:

    Zero at (1,1) sets row 1 and col 1 zero = A [OK]
Hint: Mark zero rows and columns using first row/column as flags [OK]
Common Mistakes:
  • Not marking first row and column correctly
  • Setting zeroes before marking all zero positions
  • Overwriting first row/column too early
4. The following code attempts to set matrix zeroes in place but has a bug. What is the bug?
int[,] matrix = {
  {1, 0, 3},
  {4, 5, 6},
  {7, 8, 9}
};

int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);

bool firstRowZero = false;
bool firstColZero = false;

// Check first row
for (int j = 0; j < cols; j++) {
    if (matrix[0, j] == 0) firstRowZero = true;
}
// Check first column
for (int i = 0; i < rows; i++) {
    if (matrix[i, 0] == 0) firstColZero = true;
}

// Use first row and column as markers
for (int i = 1; i < rows; i++) {
    for (int j = 1; j < cols; j++) {
        if (matrix[i, j] == 0) {
            matrix[i, 0] = 0;
            matrix[0, j] = 0;
        }
    }
}

// Set zeroes based on markers
for (int i = 1; i < rows; i++) {
    for (int j = 1; j < cols; j++) {
        if (matrix[i, 0] == 0 || matrix[0, j] == 0) {
            matrix[i, j] = 0;
        }
    }
}

// Set first row zero if needed
if (firstRowZero) {
    for (int j = 0; j < cols; j++) matrix[0, j] = 0;
}
// Set first column zero if needed
if (firstColZero) {
    for (int i = 0; i < rows; i++) matrix[i, 0] = 0;
}
medium
A. The first row zero check is done after modifying the matrix, causing incorrect results.
B. The first column zero check is missing.
C. The code does not mark zeroes in the first row and column correctly.
D. The code does not handle the case when the first element matrix[0,0] is zero.

Solution

  1. Step 1: Understand the role of matrix[0,0]

    matrix[0,0] is used to mark both first row and first column zero status, but here separate booleans are used.
  2. Step 2: Identify missing handling of matrix[0,0]

    If matrix[0,0] is zero, it affects both first row and column, but code only uses booleans, so it may miss this case.
  3. Final Answer:

    The code does not handle the case when the first element matrix[0,0] is zero. -> Option D
  4. Quick Check:

    matrix[0,0] zero affects both row and column flags = C [OK]
Hint: Remember matrix[0,0] marks both first row and column zero [OK]
Common Mistakes:
  • Ignoring matrix[0,0] special role
  • Checking first row/column after modifying matrix
  • Not using separate flags for first row and column
5. Given a matrix:
int[,] matrix = {
  {0, 1, 2, 0},
  {3, 4, 5, 2},
  {1, 3, 1, 5}
};

Apply the Set Matrix Zeroes In Place algorithm. What is the final matrix state?
hard
A. [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
B. [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
C. [[0, 0, 0, 0], [3, 0, 0, 0], [1, 0, 0, 0]]
D. [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Solution

  1. Step 1: Identify zeroes and mark rows and columns

    Zeroes at matrix[0,0] and matrix[0,3]. Mark first row and columns 0 and 3.
  2. Step 2: Set zeroes based on markers

    Set entire first row zero. Set columns 0 and 3 zero in all rows.
  3. Final Answer:

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

    Zeroes in first row set cols 0 and 3 zero = B [OK]
Hint: Zero in first row sets entire row and columns zero [OK]
Common Mistakes:
  • Not zeroing columns 0 and 3 in all rows
  • Leaving first row partially unchanged
  • Confusing zeroing rows vs columns
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