Adobe Interview Question for Software Engineer / Developers






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

Don't u think its very easy!!
int index=-1;//index where the first zero has occured

for(i=0;i<n;i++){
if(!a[i]){
if(index==-1) index =i;
continue;
}
else{
if(index==-1)continue;
else swap(arr[index],arr[i]);
index++;
}
}

- python.c.madhav November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

index should be initilaised to -1 in else statement.

- Anonymous August 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first..remove all zeroes in between d array..(like we remove spaces in a string)..O(n)
keep track of no of zeroes..using a count variable..
second concatenate dis array wid no of zeroes in d original array..

- dev November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main()
{
int arr[]={1,0,2,0,3,0,0,4,5,6,7,0,0,0},find,count;
// int arr[]={1,0,2,0,3,0,0,4,5,6,7},find,count;
int i,j;

for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
j=0;
if(arr[i] == 0)
{
find=i;
j=i;
count++;
while(arr[j] <= 0)
{
j++;
}
if(j !=sizeof(arr)/sizeof(arr[0])) //for boundary condiation checking
{
arr[find]=arr[j] ;
arr[j]=0;
}
}
}
for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
cout<<"element ="<<arr[i]<<"\t"<<endl;
}

- Anonymous November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

cant we do this ???

for(i=0;i<n;i++)
{
int flag=0;
while(flag==0)
{
if(a[i]==0)
     flag=1;
else
     i++;
}
temp=a[i];
a[i]=a[i+1]
a[i+1]=temp;
}

- gowtham December 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry,count is not required....

- Anonymous November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main()
{

    int b[] = {0,0,2,0,3,0,0,4,5,6,7,0,0,8};
    i = 0; j = 1;
    int sizeOfArray = sizeof(b) / sizeof(b[0]);
  
    while(j < sizeOfArray - 1)
    {
           while(b[i] != 0) 
             i++;
             
           while(b[j] ==0) 
             j++;
             
           b[i] = b[j] ;
           b[j] = 0;
           }
   for (int i = 0; i <=  13; i++) 
    { cout << b[i] << " ";}
}

- JoshMachine November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
            int [] array = {0,0,2,0,3,0,0,4,5,6,7,0,0,8};
          
            int hole = 0;
            for (int j = 0; j < array.Length; j++)
            {
                if (array[j] != 0)
                {
                    array[hole++] = array[j];
                    array[j] = 0;
                }
            }

            Console.WriteLine("\n After Moving zeroes to end...");
            for (int i = 0; i < hole; i++)
                Console.Write(array[i].ToString() + ", ");

}

- Mi Jalgaonkar November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this solution does not work, try it with int [] array = {1,0,0,2,0,3,0,0,4,5,6,7,0,0,8};

this works:

public static void Main(String[] args)
	{
		int [] array = {1,0,0,2,0,3,0,0,4,5,6,7,0,0,8};
		
        for (int j = 0; j < array.length; j++)
        {
        	if(array [j] == 0)
        	{
        	
	        	for(int k = j; k < array.length; k++)
	        	{
	        		if(array[k] != 0)
	        		{
	        			array[j] = array[k];
	        			array[k] = 0; 
	        			break;
	        		}
	        	}
        	}
        }

	for(int i = 0; i < array.length; i++)
	System.out.println("" + array[i]);
	}

- Anonymous February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction:

static void Main(string[] args)
        {
            int[] array = { 1, 0, 0, 2, 0, 3, 0, 0, 4, 5, 6, 7, 0, 0, 8 }; 
            int hole = 0; 
            for (int j = 0; j < array.Length; j++) 
            { 
                if (array[j] != 0) 
                {
                    int temp = array[hole];
                    array[hole++] = array[j];
                    array[j] = temp;
                } 
            }

            Console.WriteLine("\n After Moving zeroes to end...");
            for (int i = 0; i < array.Length; i++) 
                Console.Write(array[i].ToString() + ", ");

        }

- Anonymous February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The Above Solution works and its takes O(n) iterations only.

- Anonymous February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<stdlib.h>
#include<stdio.h>
#define SIZE 16
int main()
{
    int arr[SIZE]={1,0,0,2,0,3,0,0,4,5,6,7,0,0,8};
    
    int i=0,j=0;
    
        printf("\nInitial Array: ");
    for(i=0;i<(SIZE-1);i++)printf(" %d",arr[i]);
    for (i=1;i<(SIZE-1);i++)
        if(arr[i]!=0)
        {
                   arr[++j]=arr[i];
                   arr[i]=0;
                   }
    
    printf("\nFinal Array:   ");
    for(i=0;i<(SIZE-1);i++)printf(" %d",arr[i]);
    system("pause");
}

- fabregas February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do inplace sorting like selection sort or some thing

- shirishreddy89 November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess I misunderstood the question.

- shirishreddy89 November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

var nums = new Array(1,0,2,0,3,0,0,4,5,6,7,0,0,0);
	var count = 0;
	for( i=0; i < nums.length; i++ )
	{
		if( nums[i-count]==0 )
		{
			nums.splice(i-count,1);
			//nums.pop();
			count++;
		}
	}

	do {
		nums.push(0);
		count--;
	}
	while( count > 0 );
	
	
	alert( nums);

- Rob November 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tr

- Anonymous December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
	int arr[]={1,0,2,0,3,0,0,4,5,6,7,0,0,0},move = 0;

	for(int i=0;i<sizeof(arr)/sizeof(arr[0]);i++) {
		if(arr[i] == 0) {
			move++;
		} else if (move > 0) {
			arr[i - move] = arr[i];
			arr[i] = 0;
		}
	}
	for(int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
		cout<<"element =\t"<<arr[i]<<endl;


//        complexity O(n)
}

- tmpid December 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi
cant we use partition algo in quicksort.
i mean with small modification that all the element whether > or < than 0 must come in left of it.

- guruji January 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that using quicksort will disturb the sorted order of other elements.

- Thinker January 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code is simple...

#include<stdio.h>
int main()
{
    int a[]={1,0,0,2,0,3,0,0,0,4,0,0,5,0,6};
    int i,ni=0,temp;
    for(i=1;i<15;i++)
    {
      if( a[i] )
      {
          while( ni< 15 && a[ni]!= 0 )
          {
                 ni++;
          }
          temp = a[i];
          a[i] = a[ni];
          a[ni]= temp; 
      }
    }
    for(i=0;i<15;i++)
     printf("%d ",a[i]);
    getch();
    return 0;
}

- Ravi February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

void Arrange(int a[], int n)
{
	int k = 0;
	for(int i = 0; i < n; i++)
	{
		k = i;
		while(a[k] == 0 && k < n)
			k++;
		if(k == n)
			break;
		if(i != k)
		{
			// 0 was encountered
			a[i] = a[k];
			a[k] = 0;
		}
	}
}
int main()
{
	int a[] = {1,2,0,2,0,3,0,0,0,4,0,0,0,0,0};
	int n = sizeof(a)/sizeof(int);
	Arrange(a,n);
	printf("\n");
	for(int i = 0; i < n; i++)
	{
		printf("%d ",a[i]);
	}
	return 0;
}

- Naorem February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int a[13]={1,0,2,0,3,0,4,5,6,0,0,7,0};
int i=0,j=0;
 

for(i=0;i<13;i++)
{
 j=i;
 while(j>=0)
 {
   if(a[j-1]==0){ a[j-1]=a[j];  a[j]=0;  j--;}
   else j--;
 }
}
for(i=0;i<13;i++)
 printf(" %d",a[i]);
return 0;

}

- govind April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

slight change t handle the case where arraye starts with ZEROs

int main()
{
int a[15]={0,0,1,0,2,0,3,0,4,5,6,0,0,7,0};
int i=0,j=0;
//initialize i to the first non zero location.
while(a[i]==0) i++;
for(;i<15;i++)
{
//shift all non zero elements to the left of the array
 j=i;
 while(j>=0)
 {
   if(a[j-1]==0){ a[j-1]=a[j];  a[j]=0;  j--;}
   else j--;
 }
}
for(i=0;i<15;i++)
 printf(" %d",a[i]);
return 0;
}

- govind April 09, 2012 | Flag


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