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?

- febinrasheed June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;
	}

}

- rodrigo.cubillos June 16, 2014 | Flag
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)

- Reza July 12, 2014 | Flag Reply
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;
		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]);
		}
		
	}

}

- rodrigo.cubillos June 07, 2014 | Flag Reply
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...

- Vishal June 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- swdev June 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.cubillos June 16, 2014 | Flag


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