Challenge - 5 Problems
Matrix Zeroes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the matrix after running this code?
Consider the following TypeScript function that sets matrix zeroes in place. What will be the state of the matrix after calling
setZeroes(matrix)?DSA Typescript
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);Attempts:
2 left
💡 Hint
Track which rows and columns contain zeroes first, then update the matrix.
✗ Incorrect
The code first marks rows and columns that contain zeroes. Then it sets all elements in those rows and columns to zero. Only the second row and second column have zeroes initially, so only those get zeroed.
❓ Predict Output
intermediateWhat is the output of this optimized in-place zeroing code?
Given this TypeScript function that uses the first row and column as markers, what will be the matrix after calling
setZeroes(matrix)?DSA Typescript
function setZeroes(matrix: number[][]): void {
const rows = matrix.length;
const cols = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < rows; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < cols; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < rows; i++) {
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 (firstColZero) {
for (let i = 0; i < rows; i++) matrix[i][0] = 0;
}
if (firstRowZero) {
for (let j = 0; j < cols; j++) matrix[0][j] = 0;
}
}
const matrix = [
[1, 2, 0],
[4, 5, 6],
[7, 0, 9]
];
setZeroes(matrix);
console.log(matrix);Attempts:
2 left
💡 Hint
Remember to zero the first row and column last if needed.
✗ Incorrect
The first row and first column contain zeroes, so they get zeroed entirely. The zeroes in the matrix cause corresponding rows and columns to be zeroed as well.
🔧 Debug
advancedWhat error does this code produce?
This TypeScript code attempts to set matrix zeroes in place but has a bug. What error will it raise when run?
DSA Typescript
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;
}
}
}
}
}
const matrix = [
[1, 2, 3],
[4, 0, 6],
[7, 8, 9]
];
setZeroes(matrix);Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The original code had i <= rows which caused out of bounds access. This corrected code uses i < rows to avoid that error.
🚀 Application
advancedHow many elements are zero after running this code?
Given the following matrix and the setZeroes function, how many elements in the matrix will be zero after the function runs?
DSA Typescript
function setZeroes(matrix: number[][]): void {
const rows = matrix.length;
const cols = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < rows; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < cols; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < rows; i++) {
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 (firstColZero) {
for (let i = 0; i < rows; i++) matrix[i][0] = 0;
}
if (firstRowZero) {
for (let j = 0; j < cols; j++) matrix[0][j] = 0;
}
}
const matrix = [
[1, 2, 3, 4],
[5, 0, 7, 8],
[0, 10, 11, 12],
[13, 14, 15, 0]
];
setZeroes(matrix);Attempts:
2 left
💡 Hint
Count zeroes after marking rows and columns, including first row and column.
✗ Incorrect
After running setZeroes, the zeroes spread to rows 0,1,2,3 and columns 0,1,3. Counting all zero elements gives 10.
🧠 Conceptual
expertWhy does using the first row and column as markers save space?
In the in-place Set Matrix Zeroes algorithm, why do we use the first row and first column of the matrix as markers instead of separate arrays?
Attempts:
2 left
💡 Hint
Think about space complexity and memory usage.
✗ Incorrect
Using the first row and column as markers means no extra arrays are needed, reducing space complexity from O(m+n) to O(1).
