Set Matrix Zeroes In Place in DSA Typescript - Time & Space Complexity
We want to know how the time needed to set matrix zeroes grows as the matrix size grows.
Specifically, how does the work change when the matrix has more rows and columns?
Analyze the time complexity of the following code snippet.
function setZeroes(matrix: number[][]): void {
const rows = matrix.length;
const cols = matrix[0].length;
let firstColHasZero = false;
for (let i = 0; i < rows; i++) {
if (matrix[i][0] === 0) firstColHasZero = true;
for (let j = 1; j < cols; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < rows; i++) {
for (let j = 1; j < cols; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (matrix[0][0] === 0) {
for (let j = 0; j < cols; j++) {
matrix[0][j] = 0;
}
}
if (firstColHasZero) {
for (let i = 0; i < rows; i++) {
matrix[i][0] = 0;
}
}
}
This code sets entire rows and columns to zero if any element in them is zero, modifying the matrix in place.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops scanning rows and columns of the matrix.
- How many times: Each nested loop runs roughly rows x columns times.
As the matrix size grows, the number of operations grows roughly with the total number of elements.
| Input Size (rows x cols) | Approx. Operations |
|---|---|
| 10 x 10 = 100 | About 200 (two passes over matrix) |
| 100 x 100 = 10,000 | About 20,000 operations |
| 1000 x 1000 = 1,000,000 | About 2,000,000 operations |
Pattern observation: Operations grow roughly in direct proportion to the number of elements.
Time Complexity: O(m x n)
This means the time needed grows linearly with the total number of elements in the matrix.
[X] Wrong: "The time complexity is O(m + n) because we only scan rows and columns separately."
[OK] Correct: Each element in the matrix is checked multiple times inside nested loops, so the total work depends on the product of rows and columns, not just their sum.
Understanding how nested loops affect time helps you explain your code clearly and shows you can analyze performance well.
"What if we used extra space to store zero positions instead of modifying the matrix in place? How would the time complexity change?"
Practice
Set Matrix Zeroes In Place problem?Solution
Step 1: Understand the problem requirement
The problem requires changing the matrix so that any row or column containing a zero is set entirely to zero.Step 2: Note the in-place constraint
This must be done without using extra space for another matrix or large data structures.Final Answer:
Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. -> Option AQuick Check:
In-place zeroing of rows and columns = Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. [OK]
- Using extra space like another matrix
- Only zeroing rows or only columns
- Not handling the in-place requirement
matrix of numbers?Solution
Step 1: Understand TypeScript 2D array syntax
A 2D array of numbers is declared asnumber[][].Step 2: Check each option
const matrix: number[][] = [[1, 0], [3, 4]]; usesnumber[][]and initializes correctly. Options B and C use incorrect types. const matrix: any = 1, 0, 3, 4; is invalid syntax.Final Answer:
const matrix: number[][] = [[1, 0], [3, 4]]; -> Option AQuick Check:
2D array type is number[][] = const matrix: number[][] = [[1, 0], [3, 4]]; [OK]
- Using number[] for 2D arrays
- Assigning arrays to number type
- Incorrect syntax for array declaration
matrix after calling setZeroes(matrix)?
function setZeroes(matrix: number[][]): void {
const rows = matrix.length;
const cols = matrix[0].length;
const rowZero = new Array(rows).fill(false);
const colZero = new Array(cols).fill(false);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 0) {
rowZero[i] = true;
colZero[j] = true;
}
}
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (rowZero[i] || colZero[j]) {
matrix[i][j] = 0;
}
}
}
}
const matrix = [
[1, 2, 3],
[4, 0, 6],
[7, 8, 9]
];
setZeroes(matrix);
console.log(matrix);Solution
Step 1: Identify zero positions
Zero found at matrix[1][1]. So row 1 and column 1 must be zeroed.Step 2: Apply zeroing to rows and columns
Row 1 becomes [0,0,0]. Column 1 elements at rows 0 and 2 become 0. Resulting matrix is [[1,0,3],[0,0,0],[7,0,9]].Final Answer:
[[1, 0, 3], [0, 0, 0], [7, 0, 9]] -> Option CQuick Check:
Zero at (1,1) zeroes row 1 and column 1 = [[1, 0, 3], [0, 0, 0], [7, 0, 9]] [OK]
- Zeroing only rows or only columns
- Not zeroing all affected cells
- Modifying matrix during first scan
function setZeroes(matrix: number[][]): void {
const rows = matrix.length;
const cols = matrix[0].length;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 0) {
for (let k = 0; k < cols; k++) {
matrix[i][k] = 0;
}
for (let k = 0; k < rows; k++) {
matrix[k][j] = 0;
}
}
}
}
}Solution
Step 1: Analyze matrix modification during iteration
The code sets zeros immediately when found, changing matrix values during scanning.Step 2: Understand impact of immediate modification
This causes new zeros to be detected incorrectly, leading to extra rows and columns zeroed.Final Answer:
It modifies the matrix while scanning, causing incorrect zeroing. -> Option BQuick Check:
Immediate zeroing during scan causes errors = It modifies the matrix while scanning, causing incorrect zeroing. [OK]
- Zeroing matrix cells while scanning
- Not using markers to track zero rows/columns
- Assuming immediate zeroing is safe
Set Matrix Zeroes In Place solution to use O(1) extra space by using the first row and first column as markers. Given the matrix:
[ [1, 2, 0], [4, 0, 6], [7, 8, 9] ]What will be the matrix after applying this optimized approach?
Solution
Step 1: Use first row and column as markers
Zeros at (0,2) and (1,1) mark first row and column accordingly.Step 2: Zero rows and columns based on markers
Row 0 and 1 become all zeros. Column 1 and 2 become zeros in all rows. Final matrix is [[0,0,0],[0,0,0],[7,0,0]].Final Answer:
[[0, 0, 0], [0, 0, 0], [7, 0, 0]] -> Option DQuick Check:
First row/column markers zero rows/columns = [[0, 0, 0], [0, 0, 0], [7, 0, 0]] [OK]
- Not handling first row and column separately
- Overwriting markers too early
- Missing zeros in last row or column
Ready to prove it?
Pass the test and this pattern moves unknown
