## Amazon Interview Question for SDE1s

Country: India
Interview Type: In-Person

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

@rodrigo. Can you please explain how the condition 'a' and 'c' are satisfied in the two for loops?

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

point A is satisfied by adding horses in order to the stables but the point C not be addressed.

my new code considering C , the algorithm is :
1. get count of changes of colors, scrolling the horses in order (A)
2. calculate the minimum product between white and black horses.
3. add horses to stables while meet with the "minimum product between white and black horses" and number of the horses remaining > to free stables (condition B and C)

``````public class Horses {

public class HorsesData{
boolean white;
int secuence;
public HorsesData(boolean w,int sec){
this.white=w;
this.secuence=sec;
}
@Override
public String toString() {
return "["+Integer.toString(secuence)+","+white+"]"	;

}

}

public static void main(String args[]){
Horses instance = new Horses();
instance.execute();
}

public void execute(){
HorsesData horses[]={new HorsesData(true, 1),new HorsesData(true, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(false, 6)};
List stables[]= {new ArrayList(),new ArrayList(),new ArrayList()};
int changesCount = getChanges(horses);
int minMulti =  ((changesCount+1)/stables.length)+(((changesCount+1)%stables.length) != 0?1:0);

for(int i =0;i<stables.length;i++){
int currentHorses[]={0,0};

}
System.out.println("stables ["+i+"] ="+stables[i]);
}

}

private int getChanges(HorsesData[] horses) {
// TODO Auto-generated method stub
int result=0;
boolean current=horses[0].white;
for (int i=1; i< horses.length;i++){
if (horses[i].white!=current){
result++;
current=horses[i].white;
}
}
return result;
}

}``````

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

you can solve this by dynamic programming.

let's say T[i,j] contains the best value when assigning horses 1...i to stables 1...j. Then this value can be computed as:

T[i,j] = min_{ j-1 <= k <= i-1} T[k,j-1] + num_of_blacks(k+1...i) * num_of_whites(k+1...i)

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

``````public class Horses {

public class HorsesData{
boolean white;
int secuence;
public HorsesData(boolean w,int sec){
this.white=w;
this.secuence=sec;
}
@Override
public String toString() {
return "HorsesData [white=" + white + ", secuence=" + secuence
+ "]";
}

}

public static void main(String args[]){
Horses instance = new Horses();
instance.execute();
}

public void execute(){
HorsesData horses[]={new HorsesData(true, 1),new HorsesData(false, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(true, 6),new HorsesData(true, 7),new HorsesData(false, 8),new HorsesData(true, 9),new HorsesData(true, 10)};
List stables[]= {new ArrayList(),new ArrayList(),new ArrayList()};
int minHorsesPerStable = horses.length/stables.length;
int numOfStableWithMaximum =horses.length % stables.length;
for(int i =0;i<numOfStableWithMaximum;i++){
for(int j=0;j<minHorsesPerStable+1;j++){

}
System.out.println("stables ["+i+"] ="+stables[i]);
}
for (int i=numOfStableWithMaximum;i<stables.length;i++){
for(int j=0;j<minHorsesPerStable;j++){

}
System.out.println("stables ["+i+"] ="+stables[i]);
}

}

}``````

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

given n horses and k stables
Since none of the stables showuld left empty and none of the horses should be left unattended , which means n >k

Now, let say the horses 1-k is placed in stable in the same order since the order of horses needs to be preserved.

1,2,3,....k
(K+K) ...K+1
2K+1 .... 3k....and so on.....

Then the condition C will be satisfied as well...

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

This algorithm does not work for the following example:
k = 3;
input: WWWBBB
minimum configuration could be: WWW B BB => sum = 0
the algorithm above gives: WW WB BB => sum = 1

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

That's right, I do not consider the condition C in my algorithm, thank you.

My corrected version here:

``````public class Horses {

public class HorsesData{
boolean white;
int secuence;
public HorsesData(boolean w,int sec){
this.white=w;
this.secuence=sec;
}
@Override
public String toString() {
return "["+Integer.toString(secuence)+","+white+"]"	;

}

}

public static void main(String args[]){
Horses instance = new Horses();
instance.execute();
}

public void execute(){
//		HorsesData horses[]={new HorsesData(true, 1),new HorsesData(false, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(true, 6),new HorsesData(true, 7),new HorsesData(false, 8),new HorsesData(true, 9),new HorsesData(true, 10)};
//		List stables[]= {new ArrayList(),new ArrayList(),new ArrayList(),new ArrayList(),new ArrayList()};
HorsesData horses[]={new HorsesData(true, 1),new HorsesData(true, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(false, 6)};
List stables[]= {new ArrayList(),new ArrayList(),new ArrayList()};
int changesCount = getChanges(horses);
int minMulti =  ((changesCount+1)/stables.length)+(((changesCount+1)%stables.length) != 0?1:0);
//TODO:ver decimales en caso de que la divsion no sea exacta

for(int i =0;i<stables.length;i++){
int currentHorses[]={0,0};

}
System.out.println("stables ["+i+"] ="+stables[i]);
}

}

private int getChanges(HorsesData[] horses) {
// TODO Auto-generated method stub
int result=0;
boolean current=horses[0].white;
for (int i=1; i< horses.length;i++){
if (horses[i].white!=current){
result++;
current=horses[i].white;
}
}
return result;
}

}``````

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.

### 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.