Amazon Interview Question for Software Engineer / Developers


Country: India




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

Can be done in one pass (iterating through array only once)::

Following is code ::

int main() {
	int arr[] = {1,2,0,1,2,0,0,2,0,1,1,1,2,2,2,0,2,2,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,1,1};
	int len = sizeof(arr)/sizeof(arr[0]), elem, tmp, zero=0, two=len-1 , i = 0;
	while (i<len && two>=i){
		elem = arr[i];
	   if(elem==0) { arr[zero++]= elem; arr[i]=1; i++;}
	   if(elem==2) { tmp = arr[two]; arr[two--]= 2; arr[i]=tmp;}
	   if(elem==1) i++;
	}
	cout << endl;
	for(i=0;i<len;i++) cout << arr[i] << " " ;
	return 0;
}

- miki December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use {0,1,2,0,1,2,0,0,2,0,1,1,1,2,2,2,0,2,2,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,1,1}; and your answer is wrong... But you are superb.. thanks

- Shahid December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction proposed by shams rectifies this problem, check it out.

- miki December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting in one pass is non-cache-oblivious and very likely to be (much) slower than just simple counting 0s, 1s and 2s and filling the array back.

- stanimir December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting in one pass is non-cache-oblivious and very likely to be (much) slower than just simple counting 0s, 1s and 2s and filling the array back.

- stanimir December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction to your code

#include <iostream>
#include <conio.h>
using namespace std;

void swap( int& x, int& y)
{
int temp = x;
x =y;
y=temp;
}

int main()
{
int arr[] = {0,1,2,0,1,2,0,0,2,0,1,1,1,2,2,2,0,2,2,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,1,1};
int i0 = 0;
int n = sizeof(arr)/sizeof(int);
int i2 = n-1;

for( int i=0; i<i2; )
{
switch (arr[i])
{
case 0:
swap(arr[i],arr[i0]);
i++;
i0++;
break;

case 2:
swap(arr[i],arr[i2]);
if(arr[i]!=2)
i++;
i2--;
break;

case 1:
i++;
break;

}
}

for (; i0<i2;)
arr[++i0] = 1;

cout << endl;
for(int i=0;i<n;i++ ) cout << arr[i] << " " ;

getch();
return 0;
}

- y so serious? January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem is a classic example of Quick3Way Sort:

sorting-algorithms(.com)/quick-sort-3-way

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

It could be done in O(n) time using count sort since the range of number is not that high.

- Anup December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Count sort takes up O(n) space. Quick sort (3-way) can do it in place in O(n lg n)

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

I think in place sorting is not possible in O(n) time
int arr[]={2,1,2,0,2,1,0};
int tmp[]={1,1,1,1,1,1,1};//stores sorted array
int zp=0,tp=arr.length-1;

for(int i=0;i<arr.length;i++)
{
if(arr[i]==0)
tmp[zp++]=0;
if(arr[i]==2)
tmp[tp--]=2;
}

- Anonymous December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's possible. Check out some of the answers or read about "Dutch National Flag" sorting.

- eugene.yarovoi December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ miki slight change in ur code

if(elem==0) { arr[zero++]= elem; if(i!=0)arr[i]=1; i++;}

- shams December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nicely pointed out, great that you could understand my code. :) Thanks

- miki December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

shams's correction is still not correct for input that has sequence of leading 0s. A slight change in miki's code would be

if(elem==0) {arr[zero++]=elem;if(seenOneBefore==1)arr[i]=1; i++;}
if(elem==1) {seenOneBefore=1; i++}

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

#include <iostream>
#include <conio.h>
using namespace std;

void swap( int& x, int& y)
{
int temp = x;
x =y;
y=temp;
}

int main()
{
int arr[] = {0,1,2,0,1,2,0,0,2,0,1,1,1,2,2,2,0,2,2,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,1,1};
int i0 = 0;
int n = sizeof(arr)/sizeof(int);
int i2 = n-1;

for( int i=0; i<i2; )
{
switch (arr[i])
{
case 0:
swap(arr[i],arr[i0]);
i++;
i0++;
break;

case 2:
swap(arr[i],arr[i2]);
if(arr[i]!=2)
i++;
i2--;
break;

case 1:
i++;
break;

}
}

for (; i0<i2;)
arr[++i0] = 1;

cout << endl;
for(int i=0;i<n;i++ ) cout << arr[i] << " " ;

_getch();
return 0;
}

- Thunder2020 December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

small correction to the code

#include <iostream>
#include <conio.h>
using namespace std;
void swap( int& x, int& y)
{
int temp = x;
x =y;
y=temp;
}

int main()
{
int arr[] = {0,1,2,1,1,1,0};
int i0 = 0;
int n = sizeof(arr)/sizeof(int);
int i2 = n-1;

for( int i=0; i<i2; )
{
switch (arr[i])
{
case 0:
swap(arr[i],arr[i0]);
i++;
i0++;
break;

case 2:
swap(arr[i],arr[i2]);
//if(arr[i]!=2)
// i++;
i2--;
break;

case 1:
i++;
break;

}
}

for (; i0<i2;)
arr[++i0] = 1;

//cout << endl;
for(int i=0;i<n;i++ ) cout << arr[i] << " " ;

getch();
return 0;
}

- y so serious? January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey80201" class="run-this">#include <iostream>
#include <conio.h>
using namespace std;

void swap( int& x, int& y)
{
int temp = x;
x =y;
y=temp;
}

int main()
{
int arr[] = {0,1,2,0,1,2,0,0,2,0,1,1,1,2,2,2,0,2,2,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,1,1};
int i0 = 0;
int n = sizeof(arr)/sizeof(int);
int i2 = n-1;

for( int i=0; i<i2; )
{
switch (arr[i])
{
case 0:
swap(arr[i],arr[i0]);
i++;
i0++;
break;

case 2:
swap(arr[i],arr[i2]);
if(arr[i]!=2)
i++;
i2--;
break;

case 1:
i++;
break;

}
}

for (; i0<i2;)
arr[++i0] = 1;

cout << endl;
for(int i=0;i<n;i++ ) cout << arr[i] << " " ;

_getch();
return 0;
}</pre><pre title="CodeMonkey80201" input="yes">
</pre>

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

main()
{
int a[] = {0, 1, 2, 0, 1, 2, 0,1, 2, 2, 2, 0 , 2, 2, 0, 1, 0};
int tail0, head1, tail1, head2;
int n, i;

tail0 = head1 = tail1 = head2 = -1;

n = sizeof(a)/ sizeof(a[0]);
for (i = 0 ; i < n; i++){
printf("%d ", a[i]);
}
printf("\n");
tail0 = 0;
for (i = 0; i < n; i++) {
switch (a[i]) {
case 0:
if (tail0 == head1) {
if (tail1 == head2) {
a[i] = 2;
head2++;
}
a[tail1] = 1;
tail1++;
head1++;
}
a[tail0] = 0;
tail0++;
break;
case 1:
if (head1 == -1) {
if (head2 == -1)
head1 = tail1 = i;
else
head1 = tail1 = head2;
}
if (tail1 == head2) {
a[i] = 2;
head2++;
}
a[tail1] = 1;
tail1++;
break;
case 2:
if (head2 == -1)
head2 = i;
a[i] = 2;
break;
}
}

}

- Harikrishnan December 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

- Count the number of 0's, 1's, 2's - O(n).
- Write in the array 0, 1, 2 as per the above counters - O(n)

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not called as sorting in one pass.
Two passes are happening here:

1) Passing the array and counting number repitions
2) Modifying the array with repitions.

It's more than O(n). Can you provide the code to prove your logic of O(n) ?

- rajeshkumar.a December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Two passes IS O(n). BTW this is Dutch national flag problem.

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

Looks like question is changed after my reply. Initial post said O(n). Now it says one pass! Anyways, it is good to see O(1*N) solution.

Thanks,
Laxmi

- olnrao December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void 3WaySort(int data[] , int size )
  {
      int low=-1,high=size;
      int index=0;

      while( index < high )
       {
          if( data[index] ==0 )
           {
              swap(data[index],data[++low]);
              index++;
           }
           else if( data[index] ==2 )
            swap(data[index],data[--high]);
           else
             index++;
           
       }


  }

- Anonymous December 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

here is the code
logic is
1: traverse the array
2:if found 1 do nothing
2: if found 0 , swap it with 1 ( on left side)
3: if found 2 swap it with last. decrement last.

void swap(int a[],int first,int second)
{
int temp;
temp=a[first];
a[first]=a[second];
a[second]=temp;
}
void arrange(int a[],int size)
{
   int zero=0,one=0,two=size-1;
   while(one<=two)
   {
     switch(a[one])
      {
        case 0:
        swap(a,zero,one);
        zero++;
        one++;
        break;
        case 1:
        one++;
        break;
        case 2:
        swap(a,one,two);
        two--;
        break;

      }
   }
}

- getjar.com/todotasklist my android app December 10, 2011 | 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