Amazon Interview Question for Software Engineer / Developers






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

First go through all the '0' and fill up the C array.

Next starting from the end, go to the start of array, keeping track of all open positions in C. Any time you hit a 1 in B, copy A[i] to one of the open positions. If no open positions, return 'no solution'.

Repeat above step but this time start from the start of array, keeping track of all open positions in C. Any time we hit -1, copy A[i] to one of the open positions that we are keeping track of. Remove the position once filled. Any time we hit -1 and open positions set is empty, return 'no solution'.

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

How you are going to select option postions ?????

- anonymous June 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Construct a bipartite graph ( with the left nodes representing indices of A and right nodes indices of C) . Make the edges according to value in C.
If the max match is equal to length of element in array , solution exist.

- Tasnim May 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain your solution.. Thanks in advance!!

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

import java.util.ArrayList;


public class three_arrays {


public static void main(String[] args) {


int[] a = {10,7,16,20,33,15,17};
int[] b = {1,-1,1,1,-1,0,-1};
int[] c = new int[7];

c = compute_a(a,b,c);


if(c==null){
System.out.println("no solution");
}
else{

for(int i=0;i<c.length;i++){

System.out.println(c[i]);

}
}

}

private static int[] compute_a(int[] a, int[] b, int[] c) {


ArrayList<Integer> ones = new ArrayList<Integer>();

for(int i=0;i<a.length;i++){

if(b[i]==0){

c[i] = a[i];

}

if(b[i]==1){

ones.add(i);

}

if(b[i]==-1){

if(ones.size()==0)
return null;
int pos = ones.remove(0);

c[i] = a[pos];
c[pos] = a[i];
}
}


if(ones.size()!=0)
return null;

return c;


}

}

- vikram c May 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code neglects some cases. check this one a={1,2,3}
and b={1,0,-1}
c should be {3,2,1} but in your case c={3,2,initial value}
also elaborate the functions you have used.

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

@vikram

the solution is wrong
for example,a is {1,2,3}, b is {1, -1,-1}, c should be {2,3,1}. However, your solution returns null.

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

What is the running time of your algorithm..?? seems O(n^2) running time??
Can we do it little better..??

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

Sorry posted wrongly over here.. wanted to post it in next solution.. sorry :)

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

Please check my code:

#include<stdio.h>

int arrange(int a[],int b[],int c[],int n)
{
	int i=0,ci=0;
	for(i=0;i<n;i++)
	{
		if(b[i] == 0)
		{
			c[i]=a[i];
		}
		if(b[i] == -1)
		{
			while(c[ci]!=0 && ci<i)
			{
				ci++;
			}
			if(c[ci]!=0 || ci>=i)
			{
				return -1;
			}
			if(b[ci]!=0)
			{
				c[ci]=a[i];
				ci++;
			}
		}
	}
	
	for(i=0;i<n;i++)
	{
		if(b[i] == 1)
		{
			while(c[ci]!=0 && ci<n)
			{
				ci++;
			}
			if(c[ci]!=0 || ci<=i)
			{
				return -1;
			}
			if(b[ci]!=0)
			{
				c[ci]=a[i];
				ci++;
			}
		}
	}
	return 0;
}


int main()
{
	int a[]={1,2,3};//{1,2,3,4,5,6,7,8};//{1,2,3,4};
	int b[]={1,0,-1};//{1,1,0,-1,1,1,-1,-1};//{1,-1,0,1};
	int c[]={0,0,0};//{0,0,0,0,0,0,0,0,0};//{0,0,0,0};
	int i=0,n=3;

	if(arrange(a,b,c,n)==-1)
	{
		printf("No Solution.\n");
	}
	else
	{	
		for(i=0;i<n;i++)
		{
			printf(" %d ",c[i]);
		}
	}
	return 0;
}

Feedbacks are welcome.

- ananthakrishnan.s.r June 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i got output as 4 7 3 8 1 2 5 6 for A={1,2,3,4,5,6,7,8} B={1,1,0,-1,1,1,-1,-1}; i hope output here to be "no solution"
i made small change on 12th line as
while( (c[ci]!=0 || b[ci]<=0) && ci<i)
i got the intended result

- master fuji June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@master fuji
How do you say "no solution".
The output is correct only, please read the question again.

- ananthakrishnan.s.r June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the running time of your algorithm..?? seems O(n^2) running time??
Can we do it little better..??

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

Yaa Anantha,, your logic works.. Thanks.. :)

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

Welcome :)
running time is O(2*n) and not O(n^2)
In First pass it will check for 0 and -1.
In Second pass it will check for 1.
Thats it.

- ananthakrishnan.s.r June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i got output as 4 7 3 8 1 2 5 6 for A={1,2,3,4,5,6,7,8} B={1,1,0,-1,1,1,-1,-1}; i hope output here to be "no solution"
i made small change on 12th line as
while( (c[ci]!=0 || b[ci]<=0) && ci<i)
i got the intended result

- master fuji June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This works as per requirements:

public int[] makeNewArray(int A[], int B[]){
		if(A.length == 0 || B.length == 0){
			return noSolution();
		}
		int C[] = new int[A.length];
		for(int i=0;i<C.length;i++){
			C[i]=-1;
		}
		int index;
		for(int i=0;i<B.length;i++){
			if(B[i]==0){
				if(C[i]==-1){
					C[i] = A[i];
				}else{
					return noSolution();
				}
			}else{
				if(B[i]==-1){
					index = spotfromBeg(C, i);
					if(-1 != index){
						C[index]=A[i];
					}else{
						System.out.println("No Solution");
						return noSolution();
					}
				}else{
					if(B[i]==1){
						index = spotfromEnd(C, i);
						if(-1 != index){
							C[index]=A[i];
						}else{
							return noSolution();
						}
					}
				}
			}
		}
		return C;
	}
	
	public int[] noSolution(){
		System.out.println("No Solution");
		return null;
	}
	
	public int spotfromEnd(int C[],int index){
		int i;
		for(i=C.length-1;i>index && C[i]!=-1;i--){
			
		}
		if(i==index){
			return -1;
		}else{
			return i;
		}
	}
	
	public int spotfromBeg(int C[],int index){
		int i;
		for(i=0;i<index && C[i]!=-1;i++){
		
		}
		if(i==index){
			return -1;
		}else{
			return i;
		}
	}

- Kshitij July 06, 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