Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Zeroing 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 this code?
Consider the following C code that sets entire rows and columns to zero if an element is zero. What will be the printed matrix after execution?
DSA C
void setZeroes(int matrix[3][3], int rows, int cols) {
    int rowFlag = 0, colFlag = 0;
    for (int i = 0; i < rows; i++) {
        if (matrix[i][0] == 0) colFlag = 1;
    }
    for (int j = 0; j < cols; j++) {
        if (matrix[0][j] == 0) rowFlag = 1;
    }
    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;
            }
        }
    }
    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;
            }
        }
    }
    if (colFlag) {
        for (int i = 0; i < rows; i++) {
            matrix[i][0] = 0;
        }
    }
    if (rowFlag) {
        for (int j = 0; j < cols; j++) {
            matrix[0][j] = 0;
        }
    }
}

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 0, 6},
        {7, 8, 9}
    };
    setZeroes(matrix, 3, 3);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
B[[1, 0, 3], [0, 0, 0], [7, 0, 9]]
C[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
D[[1, 0, 3], [0, 0, 0], [0, 0, 0]]
Attempts:
2 left
💡 Hint
Check how the first row and first column are used as markers and how the flags affect them.
Predict Output
intermediate
2:00remaining
What is the output after setting zeroes in this 4x4 matrix?
Given the following matrix and the same setZeroes function, what is the matrix after the function call?
DSA C
int matrix[4][4] = {
    {1, 2, 3, 4},
    {5, 0, 7, 8},
    {9, 10, 11, 12},
    {0, 14, 15, 16}
};
setZeroes(matrix, 4, 4);
A[[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]]
B[[1, 0, 3, 4], [0, 0, 0, 0], [9, 0, 11, 12], [0, 0, 0, 0]]
C[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
D[[1, 2, 3, 4], [5, 0, 7, 8], [9, 10, 11, 12], [0, 14, 15, 16]]
Attempts:
2 left
💡 Hint
Remember the first row and column are markers and flags control zeroing of them.
🔧 Debug
advanced
2:00remaining
Why does this code cause incorrect zeroing in some cases?
This code attempts to set matrix zeroes in place but sometimes fails. What is the main logical error?
DSA C
void setZeroes(int** matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (matrix[i][j] == 0) {
                for (int k = 0; k < cols; k++) matrix[i][k] = 0;
                for (int k = 0; k < rows; k++) matrix[k][j] = 0;
            }
        }
    }
}
AIt only zeroes rows but not columns.
BIt does not check for zeros in the first row and column separately.
CIt uses double pointers incorrectly causing segmentation faults.
DIt modifies the matrix while scanning, causing new zeros to affect later iterations incorrectly.
Attempts:
2 left
💡 Hint
Think about what happens when you change the matrix while looping through it.
Predict Output
advanced
2:00remaining
What is the output of this 2x2 matrix after setZeroes?
Given this 2x2 matrix and the setZeroes function, what is the matrix after execution?
DSA C
int matrix[2][2] = {
    {0, 1},
    {1, 1}
};
setZeroes(matrix, 2, 2);
A[[0, 0], [1, 1]]
B[[0, 0], [0, 0]]
C[[0, 0], [0, 1]]
D[[0, 1], [1, 1]]
Attempts:
2 left
💡 Hint
Check how the first row and first column flags affect zeroing.
🧠 Conceptual
expert
2:00remaining
Why is using the first row and column as markers optimal for in-place zeroing?
In the in-place set matrix zeroes algorithm, why do we use the first row and first column to store markers instead of extra memory?
AIt reduces space complexity to O(1) by reusing existing matrix space without extra arrays.
BIt simplifies the code by avoiding nested loops.
CIt improves time complexity from O(n^3) to O(n^2).
DIt allows parallel processing of rows and columns.
Attempts:
2 left
💡 Hint
Think about memory usage and how markers are stored.