AfterAcademy Tech
•
14 Nov 2019

Difficulty: Medium
Asked in: Amazon, Google
Problem Description: Given a matrix, A of size M x N of 0's and 1's. If an element is 0, set its entire row and column to 0.
For Example:
Input:
[ [1, 0, 1],
[1, 1, 1],
[1, 1, 1] ]
Output:
[ [0, 0, 0],
[1, 0, 1],
[1, 0, 1] ]
Input:
[ [1, 0, 1],
[1, 1, 1],
[1, 0, 1] ]
Output:
[ [0, 0, 0],
[1, 0, 1],
[0, 0, 0] ]
Possible follow-up questions to ask the interviewer:-
We are discussing three ways to solve this problem:
Common mistake
A simple solution that comes to mind for this problem is that we can traverse the matrix and for every ‘0’ we encounter, we update all values in its row and column to 0. This brute force solution seems quite easy but may actually lead to a wrong answer. (How? Think!)
Correct Solution idea
One way would be to create an auxiliary matrix of the same size filled with 1’s. We could make all changes required in this auxiliary matrix and then copy it to the original matrix in the end. We search for zeroes in the original matrix and update rows and columns in the auxiliary matrix.
Solution Steps
Pseudo-Code
void setMatrixZeros(int A[][], int M, int N)
{
int temp[M][N]
for (i = 0 to M-1)
for (j = 0 to N-1)
temp[i][j] = 1
for(i = 0 to M - 1)
{
for (j = 0 to N - 1)
{
if (A[i][j] == 0)
{
for (k = 0 to M - 1)
temp[k][j] = 0
for (k = 0 to N - 1)
temp[i][k] = 0
}
}
}
for (i = 0 to M-1)
for (j = 0 to N-1)
arr[i][j] = temp[i][j]
}
Complexity Analysis
For every cell, we are traversing its row and column and there are a total of N*M cells.
Time Complexity: O(N*M*(N+M)) (Think!)
Space Complexity: O(N*M), for storing the auxiliary matrix.
Critical ideas to think!
Instead of using an auxiliary matrix, we could just store the rows and columns which need to be updated (Think!).
We could use a Hash Table to do so. We shall create a hash table for all rows and columns and set them to false. If the value of any row or column in hash is later updated to true, it's a signal for us to update that particular row or column’s value to 0.
Solution Steps
Pseudo Code
void setMatrixZeros(int A[][], int M, int N)
{
bool row[M], col[N]
for(i = 0 to M-1)
row[i] = false
for(j = 0 to N-1)
col[j] = false
for(i = 0 to M-1)
{
for(j = 0 to N-1)
{
if(A[i][j] == 0)
{
row[i] = true
col[j] = true
}
}
}
for(i = 0 to M-1)
{
for(j = 0 to N-1)
{
if(row[i] == true or col[j] == true)
A[i][j] = 0
}
}
}
Complexity Analysis
Time Complexity: Creating and filling values in 2 Hash Tables + Traversing the matrix + Updating the matrix
= O(N+M) + O(N*M) + O(N*M)
= O(N*M)
Space Complexity: O(M + N), for storing hash tables.
Critical ideas to think!
We are using extra space to store Hash Tables of rows and columns. We can optimize this space by trying to store hash for row and column in the matrix itself. (How?)
We can use the first row and first column of the matrix to store the status of the rows and columns respectively. The problem will occur for the status of first row and column which can be handled using two variables which will store the status of the first row and column (Think!)
Solution Steps
void setMatrixZeros(int A[][], int M, int N)
{
for(j = 0 to N-1)
if(A[0][j] == 0)
firstRow = true
for(i = 0 to M-1)
if(A[i][0] == 0)
firstCol = true
for(i = 0 to M-1)
{
for(j = 0 to N-1)
{
if(A[i][j] == 0)
{
A[i][0] = 0
A[0][j] = 0
}
}
}
for (i = 1 to M-1)
{
for (j = 1 to N-1)
{
if(A[0][j] == 0 || A[i][0] == 0)
A[i][j] = 0
}
}
if(firstRow == true)
{
for( i = 0 to N-1 )
A[0][i] = 0
}
if(firstCol == true)
{
for(i = 0 to M-1)
A[i][0] = 0
}
}
Complexity Analysis
Time Complexity: Checking the first row and first column + Traversing and updating the matrix + Updating first row and first column.
= O(M) + O(N) + O(M*N) + O(M) + O(N)
= O(M*N)
Space Complexity: O(1)
Critical ideas to think!

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
The problem is to find the median in a row-wise sorted matrix. This is an interview problem asked in companies like Amazon. There is a further constraints on extra space. We have discussed two possible solutions.

AfterAcademy Tech
Given an array A[] of n elements filled with several integers, some of them being zeroes, you need to move all the zeroes to the end.

AfterAcademy Tech
This is a mathematical problem asked in interviews of Google like companies. For solving such problems you need to have some math skills and also some idea of algorithms like Euclid's GCD algorithm.

AfterAcademy Tech
This is an interview problem asked in companies like Google, Microsoft, Amazon. The problem is to remove duplicates from a sorted array or list.
