AfterAcademy Tech
•
12 Nov 2019

Level: Easy
Asked in: Facebook, Uber
Problem Description: 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.
For example :
Input: A[] = {1, 8, 3, 0, 2, 0, 1, 10, 13, 0}
Output: {1, 8, 3, 2, 1, 10, 13, 0, 0, 0}
Input: A[] = {0, 3, 5, 9, 0, 0, 23, 2}
Output: {3, 5, 9, 23, 2, 0, 0, 0}
Possible questions to ask the interviewer:-
We will be discussing three possible solutions for this problem:-
We can create an auxiliary array B[] of size n which will be used to store all the non-zero elements as we linearly traverse A[]. Let the number of non-zero elements be x. Now fill the remaining n-x elements with zeroes and return B[].
Pseudo-Code
int[] moveZeroes(int A[], int n)
{
int B[n]
int j = 0
for (i = 0 to n-1)
{
if (A[i] != 0)
{
B[j] = A[i]
j = j + 1
}
}
while (j < n)
{
B[j] = 0
j = j + 1
}
return B
}
Complexity Analysis
Time Complexity: Filling non-zero elements in B[] + Filling zeroes in B[] = O(n) + O(n) (Why?) = O(n)
Space Complexity: O(n), for storing B[]
Critical ideas to think!
A two-pointer approach could be used for these types of questions. A pointer i to linearly traverse A[] and a pointer j to track the non-zero element. After the whole process, all elements in A[0…j] are non-zero elements.
Solution Steps
Pseudo-Code
int[] moveZeroes(int A[], int n)
{
int j = 0
for (i = 0 to n-1)
{
if (A[i] != 0)
{
A[j] = A[i]
j = j + 1
}
}
while (j < n)
{
A[j] = 0
j = j + 1
}
return A
}
Complexity Analysis
Time Complexity: Traversing A[] for non-zero elements + Traversing A[] again to fill zeroes = O(n) + O(n) = O(n)
Space Complexity: O(1)
Critical ideas to think!
In the above method, every time we encounter a non-zero element, we assign A[j] to A[i]. Instead, if we swap the two elements, we wouldn’t need to fill the rest of the array with zeroes in the end. They will be moved to the end due to swapping. This idea is similar to the partition process in the quick sort.
Solution Steps
Pseudo-Code
int[] moveZeroes(int A[], int n)
{
int j = 0
for(i = 0 to n-1)
{
if(A[i]!= 0)
{
swap(A[j], A[i])
j = j + 1
}
}
return A
}
Complexity Analysis
Time Complexity: Linear traversal of the array with swap operations = O(n) (Why?)
Space Complexity: O(1)
Critical ideas to think!

Please comment down below if you have alternative approaches or find an error/bug in the above approaches.
AfterAcademy Tech
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

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.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft. The problem is to merge two binary trees. The overlapping nodes are replaced by the sum of corresponding nodes. We are discussing two ways of solving this.
