0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 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 matrix below, what will be its state after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [
  [1, 2, 3],
  [4, 0, 6],
  [7, 8, 9]
]

# After running the algorithm, print the matrix row-wise
A[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
B[[1, 0, 3], [0, 0, 0], [0, 0, 0]]
C[[1, 0, 3], [0, 0, 0], [7, 0, 9]]
D[[1, 0, 3], [4, 0, 6], [7, 0, 9]]
Attempts:
2 left
💡 Hint
Remember that if an element is zero, its entire row and column become zero.
Predict Output
intermediate
2:00remaining
What is the output matrix after applying the algorithm?
Consider this matrix. What will it look like after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [
  [0, 1, 2, 0],
  [3, 4, 5, 2],
  [1, 3, 1, 5]
]

# Print the matrix row-wise after the algorithm
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], [0, 0, 0, 0], [0, 0, 0, 0]]
D[[0, 0, 0, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
Attempts:
2 left
💡 Hint
Zeros in the first row cause their columns and rows to be zeroed.
🔧 Debug
advanced
2:00remaining
What error does this code raise when run?
This code tries to set matrix zeroes in place. What error will it raise?
DSA Python
def set_zeroes(matrix):
    rows = len(matrix)
    cols = len(matrix[0])
    zero_rows = set()
    zero_cols = set()

    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] == 0:
                zero_rows.add(i)
                zero_cols.add(j)

    for i in zero_rows:
        for j in range(cols):
            matrix[i][j] = 0

    for j in zero_cols:
        for i in range(rows):
            matrix[i][j] = 0

matrix = [[1,2,3],[4,0,6],[7,8,9]]
set_zeroes(matrix)
print(matrix[3])
ATypeError: 'int' object is not subscriptable
BIndexError: list index out of range
CNo error, prints [7, 8, 9]
DAttributeError: 'set' object has no attribute 'append'
Attempts:
2 left
💡 Hint
Check the print statement accessing matrix[3].
🧠 Conceptual
advanced
2:00remaining
How many elements are set to zero after running the algorithm on this matrix?
Given the matrix below, how many elements will be zero after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [
  [1, 2, 0],
  [4, 5, 6],
  [0, 8, 9]
]
A6
B5
C4
D8
Attempts:
2 left
💡 Hint
Count zeros in rows and columns affected by zeros.
🚀 Application
expert
3:00remaining
What is the final matrix after applying the algorithm in place?
Apply the Set Matrix Zeroes In Place algorithm on this matrix and provide the final matrix state.
DSA Python
matrix = [
  [1, 2, 3, 4],
  [5, 0, 7, 8],
  [9, 10, 11, 12],
  [0, 14, 15, 16]
]

# Print matrix row-wise after algorithm
A[[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]]
B[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
C[[1, 0, 3, 4], [5, 0, 7, 8], [9, 0, 11, 12], [0, 0, 0, 0]]
D[[0, 2, 3, 4], [0, 0, 0, 0], [0, 10, 11, 12], [0, 0, 0, 0]]
Attempts:
2 left
💡 Hint
Zeros at (1,1) and (3,0) affect their rows and columns.