Facebook Interview Question for Android Engineers


Team: London office
Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void main(String[] args)
  {
    int arr1[] = {0};
	int arr2[] = {0, 1};
	int arr3[] = {0, 1, 0};
	int arr4[] = {1, 0};
	int arr5[] = {1, 0, 0};
	int arr6[] = {0, 1, 0, 2};
	int arr7[] = {1, 0, 0, 2};

    System.out.println(Arrays.toString(moveAndPrint(arr1)));
    System.out.println(Arrays.toString(moveAndPrint(arr2)));
    System.out.println(Arrays.toString(moveAndPrint(arr3)));
    System.out.println(Arrays.toString(moveAndPrint(arr4)));
    System.out.println(Arrays.toString(moveAndPrint(arr5)));
    System.out.println(Arrays.toString(moveAndPrint(arr6)));
    System.out.println(Arrays.toString(moveAndPrint(arr7)));
    
  }
  
  public static int[] moveAndPrint(int[] arr)
  {
    if (arr == null) return null;
    
    int i = 0, j = arr.length-1;
    
	while (i < j) {
		while (arr[j] == 0)
			j--;

		while (arr[i] != 0)
			i++;

      	// Swap elements
      	if (i < j) {
        	arr[i] = arr[j];
        	arr[j] = 0;
        }
	}

    return arr;
  }

- Tejas April 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use two pointers, one for iterating through the array and another one for maintaining the last seen zero index. Every time you see a non-zero index, swap it with the previous seen zero index. Simple solution in Python

Solution:

def moveZeroes(arr):
    i, j = 0, 0
    while i < len(arr):
        if arr[i] != 0:
            arr[i], arr[j] = arr[j], arr[i]
            j += 1
        i += 1
    return arr

Test code:

print(moveZeroes([0, 3, 1, 0, 2, 6, 7, 0])) # Output: [3, 1, 2, 6, 7, 0, 0, 0]
print(moveZeroes([0,0,1,0,0])) # Output: [1, 0, 0, 0, 0]
print(moveZeroes([0,3])) # Output: [3, 3]

- prudent_programmer March 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def none_zero(array):
j = 0
for i in range(len(array)):
if array[i] != 0:
array[j] = array[i]
j += 1
return array[:j]

print none_zero([1,0,3,2,6,7,1,0,10,0])

- two pointers method is good enough March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def none_zero(array):
  j = 0
  for i in range(len(array)):
    if array[i] != 0:
      array[j] = array[i]
      j += 1
  return array[:j]

print none_zero([1,0,3,2,6,7,1,0,10,0])

- two pointers March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def none_zero(array):
  j = 0
  for i in range(len(array)):
    if array[i] != 0:
      array[j] = array[i]
      j += 1
  return array[:j]

print none_zero([1,0,3,2,6,7,1,0,10,0])

- two pointers March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class HelloWorld{

public static void main(String []args){
int[] arr = {1, -2, 5, 6, 0, 1,0,3,0,0,0, 6,8,9};
for(Integer a: moveAndPrint(arr)){
System.out.println(a);
}
}

private static int[] moveAndPrint(int[] arr){
int i =0;
for(int j=arr.length-1; i<j; j--){
if(arr[i] != 0){
if(arr[j] == 0){
arr[j] = arr[i];
arr[i] = 0;
i++;
}
}
}
System.out.println(arr.length - i);
return arr;
}
}

- Anonymous March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class HelloWorld{

public static void main(String []args){
int[] arr = {1, -2, 5, 6, 0, 1,0,3,0,0,0, 6,8,9};
for(Integer a: moveAndPrint(arr)){
System.out.println(a);
}
}

private static int[] moveAndPrint(int[] arr){
int i =0;
for(int j=arr.length-1; i<j; j--){
if(arr[i] != 0){
if(arr[j] == 0){
arr[j] = arr[i];
arr[i] = 0;
i++;
}
}
}
System.out.println(arr.length - i);
return arr;
}
}

- dda March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
         int[] arr = {1, -2, 5, 6, 0, 1,0,3,0,0,0, 6,8,9};
         for(Integer a: moveAndPrint(arr)){
             System.out.println(a);
         }
     }
     
     private static int[] moveAndPrint(int[] arr){
         int i =0;
         for(int j=arr.length-1; i<j; j--){
             if(arr[i] != 0){
                 if(arr[j] == 0){
                     arr[j] = arr[i];
                     arr[i] = 0;
                     i++;
                 } 
             }
         }
         System.out.println(arr.length - i);
         return arr;
     }

}

- Anonymous March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fails on [1, 0, 2, 0,0,1,0,0,0]

- Nitu October 12, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using above test inputs. 

public static void main(String[] args){
        int[] arr = {0, 3, 1, 0, 2, 6, 7, 0};
        System.out.println(Arrays.toString(nonZero(arr)));
        
        int[] arr1 = {0, 0, 1, 0, 0, 0};
        System.out.println(Arrays.toString(nonZero(arr1)));
        
        int[] arr2 = {0, 3};
        System.out.println(Arrays.toString(nonZero(arr2)));
        
    }
    
    public static int[] nonZero(int[] arr) {
        int i=0;
        int length = arr.length;
        int result[] = new int[length];
        int j=0;
        while(i<length){
            if(arr[i] == 0) {
                i++;
            }else{
                result[j++] = arr[i++];
            }
        }
        return result;
    }

Results
[3, 1, 2, 6, 7, 0, 0, 0]
[1, 0, 0, 0, 0, 0]
[3, 0]

- silvimasss March 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MoveZerosToLast {
	public static void main(String[] args) {
		int[] a = { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9 };
		int j = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] != 0) {
				if (i == j) {
					j++;
				} else {
					a[j] = a[i];
					a[i] = 0;
					j++;
				}
			}
		}
		System.out.println(Arrays.toString(a));
	}
}

- Bushan SC March 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from sys import stdin
array = stdin.readline().strip().split()
ceros=0
for i in array:
  if i == '0':
    ceros += 1
  else:
    print i,
print '0 ' * ceros

- Anonymous March 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from sys import stdin
array = stdin.readline().strip().split()
ceros=0
for i in array:
  if i == '0':
    ceros += 1
  else:
    print i,
print '0 ' * ceros

- NicolasZON March 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args)
  {
    int arr1[] = {0};
	int arr2[] = {0, 1};
	int arr3[] = {0, 1, 0};
	int arr4[] = {1, 0};
	int arr5[] = {1, 0, 0};
	int arr6[] = {0, 1, 0, 2};
	int arr7[] = {1, 0, 0, 2};

    System.out.println(Arrays.toString(moveAndPrint(arr1)));
    System.out.println(Arrays.toString(moveAndPrint(arr2)));
    System.out.println(Arrays.toString(moveAndPrint(arr3)));
    System.out.println(Arrays.toString(moveAndPrint(arr4)));
    System.out.println(Arrays.toString(moveAndPrint(arr5)));
    System.out.println(Arrays.toString(moveAndPrint(arr6)));
    System.out.println(Arrays.toString(moveAndPrint(arr7)));
    
  }
  
  public static int[] moveAndPrint(int[] arr)
  {
    if (arr == null) return null;
    
    int i = 0, j = arr.length-1;
    
	while (i < j) {
		while (arr[j] == 0)
			j--;

		while (arr[i] != 0)
			i++;

      	// Swap elements
      	if (i < j) {
        	arr[i] = arr[j];
        	arr[j] = 0;
        }
	}

    return arr;

}

- Tejas April 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int nonzero(vector<int> & arr)
{
	int n = arr.size(), i = 0, j = n - 1;
	while (i < j)
	{
		while (j>=0 && arr[j] != 0)
			j--;
		while (i<n && arr[i] == 0)
			i++;
		if (i>j||j<0||i>=n)
			return n-(j + 1);
		iter_swap(arr.begin() + i, arr.begin() + j);
	}
}

- aman.bansal4u May 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[3,2,0,3,1,3,0,0,1,0]
b=[None]*len(a)
def movezeroo(a):
i=0
j=len(a)-1
k=0
print(b)
while i<=len(a)-1:
if a[i]!=0:
print(i, j,'ok')
b[j]=a[i]
print(i,j)
i=i+1
j=j-1
else:
b[k]=a[i]
k=k+1
i=i+1
print(b)

- Harsh October 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[3,2,0,3,1,3,0,0,1,0]
b=[None]*len(a)
def movezeroo(a):
    i=0
    j=len(a)-1
    k=0
    print(b)
    while i<=len(a)-1:
        if a[i]!=0:
           print(i, j,'ok')
           b[j]=a[i]
           print(i,j)
           i=i+1
           j=j-1
        else:
            b[k]=a[i]
            k=k+1
            i=i+1
    print(b)

- Harsh October 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[3,2,0,3,1,3,0,0,1,0]
b=[None]*len(a)
def movezeroo(a):
i=0
j=len(a)-1
k=0
print(b)
while i<=len(a)-1:
if a[i]!=0:
print(i, j,'ok')
b[j]=a[i]
print(i,j)
i=i+1
j=j-1
else:
b[k]=a[i]
k=k+1
i=i+1
print(b)

- Anonymous October 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
	
public class MoveNonZeroElementsToEndOfArray {
	public static void main(String[] args){
		int test1[] = new int[] {1,0,-2,0,3};
		int test2[] = new int[] {0,0};
		int test3[] = new int[] {1,2,3};
		
		System.out.println(Arrays.toString(test1));
		compactify(test1);
		System.out.println(Arrays.toString(test1));
		
		System.out.println(Arrays.toString(test2));
		compactify(test2);
		System.out.println(Arrays.toString(test2));
		
		System.out.println(Arrays.toString(test3));
		compactify(test3);
		System.out.println(Arrays.toString(test3));
   }
   
   public static int compactify(int[] inp) {
	   int n = inp.length;
	   int destIndex = -1;
	   for (int i = n-1; i >= 0; i--) {
		   if ((destIndex > 0) && (inp[i] != 0)){
			   // swap inp[i] and inp[destIndex]
			   int tmp = inp[i];
			   inp[i] = inp[destIndex];
			   inp[destIndex] = tmp;
			   destIndex--;
		   } else if ((destIndex < 0) && (inp[i] == 0)) {
			   destIndex = i;
		   }
	   }
	   System.out.println("#non zero " + (n-(destIndex+1)));
	   return destIndex+1;
   }
}

- just_do_it November 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void reArrange(int[] nums) {
      int i=0, j=nums.length-1;
      while( j> 0 && nums[j] > 0) {
          j--;
      }
      while( i < j) {
         if(nums[i] > 0) {
             int tmp = nums[j];
             nums[j]=nums[i];
             nums[i]=tmp;
             j--;
             while( j> 0 && nums[j] > 0) {
                j--;
            }
         }
         i++;
      }
   }

- Nitu October 12, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More