Amazon Interview Question for Software Engineer / Developers






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

one possible solution could be just count the number of zero one and twos.
And overwrite the whole array with 0 ,1 or 2

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

Another way could be to pick up first '1' as a pivot element and do a quick sort over the array.

- Amit Grover November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 option is much better. It is counting sort and happens in linear time.

- shekhar2k4 November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first option(counting) is much better. It is counting sort and happens in linear time.

- shekhar2k4 November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

linerly traverse the array and add the number to the link list and keep track of the pointer to the last 0 , 1 and 2

Keep on appending the numbers and update the last pointers.

Thus, O(n)

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

Option 1 is bad since the complexity is O(N^2), first traverse each item to count number of 0s, 1s and 2 and then again tarverse the array to set 0,1 and 2 in correct position.

Option 2: Quick sort or Merge Sort will give N(LogN) complexity.

Option 3: Using a Linked List: best performance since it will be O(n), however it needs extra spaces for the Linked List and also for the 0,1,2 s pointer. However, the question mentioned 'Overwrite the whole array', so considering that fact we have to rewrite the Array by traversing the Linked List.

So, so far the Sort( Qucik or Merge )is a better solution that the others.

- Gautam November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if u think first option is O(nxn) u r smokin some good shit!!

- samyzee November 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gautam
Chutiye

- :P November 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Gautam
You can get the count of the number of 0's,1's and 2's just by traversing the array once.This is O(n)and not O(n^2).

- Ran November 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Continued from Ran...
and setting them in the array will also take O(n), so thats
O(n) + O(n) = O(2n).

- Anonymous November 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gautam, very funny :):)

- Mac January 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep track of 0's index (start at zero and increment when 0 found)
Keep track of 2's index (start at end of array, decrement when 2's found)
Keep count of 1's,
loop 0's index < 2's index

fill 1's from 0's index to 2's index

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

This is same as Dutch Flag Algorithm.

- Rbn November 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

to upkaar kiyaa bataake chutiye :)

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what wud be the ans when there is an order constraint(means u dont have to change the order of 0's and 1's).
This one is actual question asked by Amazon..

- raghav.yo December 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think just apply counting sort. It will also take care of the order since it is a stable sort. Running time for this wud be O(2n)

- Ranjit December 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dutch National flag complexity O(n).

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

aur 1 dutch waala chutiyaa

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#define MAX 11
// Solution seed by Sandy Grace, NN Priya, Vaishnavi, Prabhakar Dhandapani
// Nurtured, Manured, Weeded, Harvested by Sridhar Arumugasamy
void main( void ) {
    int arr[MAX] = {0,1,2,0,1,2,0,1,2,0,1},ctr,stop;
    for ( ctr = 0 ; ctr < MAX ; ctr++ )
    stop= arr[ctr] > 3  ? (arr[ arr[ ctr] % 3 ] += 3) : (arr[arr[ctr]] += 3) ;
    stop = arr[2] / 3;
    for ( ctr = MAX ; ctr >= MAX - stop ;  ctr-- ) arr[ctr] = 2 ;
    stop += arr[1] / 3 ;
    for ( ; ctr >= MAX - stop ; ctr -- )          arr[ctr] = 1;
    for( ; ctr > -1 ; ctr-- ) 	                 arr[ ctr ] = 0 ;
 }

- FindMind August 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Sort012 {
public static void main(String[]args){
int arr[]={2,1,2,1,1,0,1,0,1,0,1,0,1,0,1,2,0,1,0,2,0,1};
System.out.println(Arrays.toString(arr));
int[] pivots = {0,1,2};
int start=0;
for (int i = 0; i < pivots.length; i++) {
start =sort012(arr,pivots[i],start);
}



System.out.println(Arrays.toString(arr));
}

private static int sort012(int[] arr,int pivot,int startIndex) {
int p=startIndex-1;
for(int i=0;i<arr.length;i++){
if(arr[i]==pivot){
p++;
swap(arr,i,p);
}
}
return p+1;


}

private static void swap(int[] arr, int k, int i) {
int temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;

}
}

- sudhansu: January 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The time complexity of above code is almost O(n).

- sudhansu: January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved by traversing the array and keeping two indexes i and j to point to location where next 0 and 2 is going to be placed.
Code is given below :-

// Sort an array containing 0s, 1s and 2s.cpp : Defines the entry point for the console application.
// Program to sort an array containing 0s, 1s and 2s only

#include<iostream>

void swap(int* num1, int* num2)
{
	int nTemp = *num1;
	*num1 = *num2;
	*num2 = nTemp;
}

int main()
{
	int nArray[] = {1, 2, 0, 0, 1, 2, 2, 2, 1, 1, 0, 0, 2, 1, 1};
	int nLength = sizeof(nArray) / sizeof(int);

	int i = 0;
	int j = nLength - 1;
	int k = 0;

	while(k < j)
	{
		if(nArray[k] == 0)
		{
			if(k != i)
			{
				swap(nArray + i, nArray + k);
			}
			k++;
			i++;
		}
		else if(nArray[k] == 2)
		{
			swap(nArray + j, nArray + k);
			j--;
		}
		else
		{
			k++;
		}
	}

	std::cout << "Array after segegating 0s, 1s and 2s\n";
	for(int i = 0; i < nLength; i++)
	{
		std::cout << nArray[i] << std::endl;
	}

	return 0;
}

- sid1505 September 06, 2015 | 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