Facebook Interview Question
348 Interview Reviews |
Back to all Facebook Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Facebook:
Given an array of integers, now we want to erase all 0's (can be other value), and we want the result array condensed, meaning no empty cell in the array.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (9)
I think this should work:
public condense(int[] iArray, int delthis){
ArrayList iList = new ArrayList();
for (int i=0; i<iArray.length; i++){
if (iArray[i] != delthis)
iList.add(iArray[i]);
}
return iList.toArray();
}
Helpful Answer?
Yes |
No
Inappropriate?
public Integer[] condense(int[] iArray, Integer delThis){
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i=0; i<iArray.length; i++){
if (iArray[i] != delThis)
iList.add(iArray[i]);
}
return iList.toArray(new Integer[iList.size()]);
}
Helpful Answer?
Yes |
No
Inappropriate?
{
int p1 = 0, p2 = 0;
while ( p1 < size && p2 < size )
{
if ( arr[p1] == 0 ) {
while ( p2 < size && arr[p2] == 0 ) {
++ p2;
}
if ( p2 == size ) {
break;
}
arr[p1] = arr[p2];
++ p1;
++ p2;
}
else {
++ p1;
++ p2;
}
}
new_size = p1+1;
}
Helpful Answer?
Yes |
No
Inappropriate?
void erase( int arr[] , int size , int &new_size )
{
int p1 = 0, p2 = 0;
while ( p1 < size && p2 < size )
{
if ( arr[p1] == 0 ) {
while ( p2 < size && arr[p2] == 0 ) {
++ p2;
}
if ( p2 == size ) {
break;
}
arr[p1] = arr[p2];
arr[p2] = 0;
++ p1;
++ p2;
}
else {
++ p1;
++ p2;
}
}
new_size = p1;
}
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
std::vector v;
v.erase(std::remove(v.begin(), v.end(), 0), v.end());
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
using namespace std;
int main()
{
int a[] = { 0,2,3,0,4,0,5,0,0,0};
int i,nz = 0;
for(i = 0; i<=9 ; i++)
{
if (a[i] == 0)
nz++;
else
a[i-nz] = a[i];
}
for(i = 0; i <=9-nz ; i++)
cout << a[i]<<" ";
return 0;
}
Helpful Answer?
Yes |
No
Inappropriate?
-------------
s = [1, 2, 4, 0, 5, 0, 1, 0, 0, 1, 7, 0, 8, 0, 2, 4, 5, 1, 2, 8, 0, 5, 6, 8]
for k in xrange(len(s), 0, -1):
if not s[k-1]: s.pop(k-1)
--------------
after:
[1, 2, 4, 5, 1, 1, 7, 8, 2, 4, 5, 1, 2, 8, 5, 6, 8]
Helpful Answer?
Yes |
No
Inappropriate?
int i = 0, j = -1;
while(i < sz && j < sz) {
if(arr[i] == errasedVal && j == -1) j = i;
else if(arr[i] != errasedVal && j != -1) {
cout << arr[i]<< " " << j << endl;
swap(arr[i], arr[j]);
j++;
while(arr[j] != errasedVal && j < sz) j++;
}
i++;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 1 people found this helpful
by MOHIT DHINGRA:
#include<conio.h>
void main()
{
clrscr();
int i,j,k.a[20];
cout<<"enter the size of array"';
cin>>n;
cout<<"enter the elements of array";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"entered array is";
for(i=0;i<n;i++)
{
cout<<a[i];
}
for(i=0;i<n;i++)
{ if(a[i]==0) /*operation for deleting zero's*/
for(j=i;j<n;j++)
{a[j]=a[j+1];
}}
cout<<"array left is";
for(i=0;i<n;i++)
{ cout<<a[i];
}
getch();
}