Amazon Interview Question for Software Engineer / Developers






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

I reached the solution, but it took me sometime,the interviewer was very helpful.But I don't think I was fast enuf..

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

public class array {
public static void main(String[] args) {
int arr[] = {1,0,0,0,0,1,1,0,0,1,1,0,0,0,0};
int noOfZeros = 0;
for(int i =0 ; i<arr.length;i++) {
if(arr[i] == 0) {
noOfZeros++;
}
}
for(int i =0 ; i<noOfZeros;i++) {
arr[i] = 0;
}
for(int i = noOfZeros ; i<arr.length;i++) {
arr[i] = 1;
}
for(int i = 0 ; i<arr.length;i++) {
System.out.print(arr[i] + " ");
}}}

- Array August 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this is not a inplace algorithm

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

Will the above solution work for this kind of problem

- Array August 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was the interviewer satisfied with this solution or does this need to be further optimised??

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

public class array {
public static void main(String[] args]){
int arr[] = {1,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0};
int i = 0;
int j = arr.length() - 1;
while ( i < j ) {
if ( arr[i] != 1 ) {
i++;
} else if ( arr[j] != 0 ) {
j++;
} else {
arr[i] = 0;
arr[j] = 1;
i++; j++;

}

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

Is j++ correct? Array out of bounds problem?

- algofreak August 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks like j++ should be j--

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

use the idea from quick sort can solve it

void sortZeroOne( int [] v, int len )
{
    int firstOne = 0;
    for ( int i = 0; i < len - 1; i++ ) {
        if ( v[i] == 0 ) {
            if ( i != firstOne ) {
                swap( v[i], v[firstOne] );
            }
            firstOne++;
        }
    }
}

}

- annoy August 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one is so good.

- Anonymous August 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is a bug in the code, it should be
for ( int i = 0; i < len ; i++ ) {

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

on this site there is always bug in code :D

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

#include<stdio.h>

int main()
{
int a[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0};
int len = 0, i = 0, j = 0;

len = (sizeof(a)/sizeof(int));
j = (len - 1);

for(;i<j;)
{
if((a[i] == 1) && (a[j] == 0))
{
a[i++] = 0;
a[j--] = 1;
}
else if(a[i] == 0)
{
i++;
}
else if(a[j] == 1)
{
j--;
}

}

for(i=0;i<len;i++)
printf("Value: %d\n", a[i]);

return 0;
}

- algofreak August 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Count the number of 0s. Let count be C.
2) Once we have count, we can put C 0s at the beginning and 1s at the remaining n – C positions in array.

Time Complexity: O(n)

The method 1 traverses the array two times. Method 2 does the same in a single pass.




Maintain two indexes. Initialize first index left as 0 and second index right as n-1.

Do following while left < right
a) Keep incrementing index left while there are 0s at it
b) Keep decrementing index right while there are 1s at it
c) If left < right then exchange arr[left] and arr[right]

And interviewer liked the first approach but asked me other ways around.he was very helpful

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

void ArrangeZerosAndOnes(char arr[], int N)
{
	int i=0, j= N-2;
	while(i<j)
	{
		while(arr[i]=='0')
			i++;
		while(arr[j]=='1')
			j--;

		arr[j--]='1';
		arr[i++]='0';
	}

	return ;
}

- chashu August 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Groovy

def sortArray(int[] a) {
    int zeroidx = -1;
    for (int i = 0; i < a.length; i++) {
        if (a[i] == 0 && i != ++zeroidx) {
            a[zeroidx] = 0;
            a[i] = 1
        }
    }
    return a
}

println sortArray([0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0] as int[])

- mrfritz379 August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey91211" class="run-this">/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package subseq;


public class Main {


public static void main(String[] args) {
//Given an array of 0s and 1s , in O(n) time and inplace,
//make all the 0s in one side and all the 1s in other.
int num[] = {1,1,1,0,0,0,0,1};
int zero = 0;
for(int i=0;i<num.length;i++)
{
if(num[i]==0)
{
zero++;
}
}

for(int j = 0,k=zero;j<num.length;j++)
{
if(zero>0)
{
System.out.println("0");
zero--;
}
else
{
System.out.println("1");
}
}

}


}

</pre><pre title="CodeMonkey91211" input="yes">
</pre>

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

only 1 pass of quick sort

- karan_nit August 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what would you take the PIVOT element as? index of first element with '0' ?

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

static void Group0sAnd1s(int[] a)
{
int i = 0;
int j = a.Length - 1;

while (i < j)
{
while (i < a.Length && a[i] == 0)
i++;

while (a[j] == 1 && j >= 0)
j--;

if (i < j && i < a.Length && j >= 0)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}

- visitor October 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This works...Do you see a case which can break this code?

void func(int a[], int n)
{
int left = a[0];
int i =0 ;int j; int temp;

for(j=1;j<n;j++)
{
if(a[j]!=left && a[j-1]==left)
i++;
if(a[j] == left && i<j)
{
temp = a[i];
a[i] = a[j];
a[j]=temp;
i++;
}
}
return;
}


}

- farhat.saiyed October 15, 2010 | 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