Amazon Interview Question
Software Engineer / DevelopersConstruct 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.
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;
}
}
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.
@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.
What is the running time of your algorithm..?? seems O(n^2) running time??
Can we do it little better..??
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.
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
How do you say "no solution".
The output is correct only, please read the question again.
What is the running time of your algorithm..?? seems O(n^2) running time??
Can we do it little better..??
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;
}
}
First go through all the '0' and fill up the C array.
- Anonymous May 25, 2011Next 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'.