Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
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 addedhorses=0;
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};
while((addedhorses<horses.length
&& ((horses.length-addedhorses)>=(stables.length-i))
&& (currentHorses[0]+(horses[addedhorses].white?1:0))*(currentHorses[1]+(horses[addedhorses].white?0:1))<minMulti )) {
currentHorses[horses[addedhorses].white?0:1]++;
stables[i].add(horses[addedhorses++]);
}
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;
}
}
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;
int addedhorses=0;
for(int i =0;i<numOfStableWithMaximum;i++){
for(int j=0;j<minHorsesPerStable+1;j++){
stables[i].add(horses[addedhorses++]);
}
System.out.println("stables ["+i+"] ="+stables[i]);
}
for (int i=numOfStableWithMaximum;i<stables.length;i++){
for(int j=0;j<minHorsesPerStable;j++){
stables[i].add(horses[addedhorses++]);
}
System.out.println("stables ["+i+"] ="+stables[i]);
}
}
}
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...
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
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 addedhorses=0;
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};
while((addedhorses<horses.length
&& ((horses.length-addedhorses)>=(stables.length-i))
&& (currentHorses[0]+(horses[addedhorses].white?1:0))*(currentHorses[1]+(horses[addedhorses].white?0:1))<minMulti )) {
currentHorses[horses[addedhorses].white?0:1]++;
stables[i].add(horses[addedhorses++]);
}
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;
}
}
@rodrigo. Can you please explain how the condition 'a' and 'c' are satisfied in the two for loops?
- febinrasheed June 12, 2014