Amazon Interview Question
Software Engineer in Testswhy to go for 7th trial for second fastest. Which ever horse will come second in 6th Trial is second fastest...
its possible that second fastest in race 6th is slower than the horse 2nd to the fasted horse
Hey consider this case - We are splitting 25 horses into groups of 5 and conducting 5 trials. What if the second fastest among 1st group is the fastest compared with the fastest horses of other groups?? So for the final trial even if we conduct only among the fastest of all the groups we are likely to miss the second fastest one which is still in the 1st group. Hence I think we might need to remove the fastest horse from its initial group. Conduct a test among its group of 4. Then conduct the fastest among all groups. So 5+1+1+1 trials might be required. Corrections are welcome!!
Lets say we segregate the horses in 5 groups - A, B, C, D and E. We conduct 5 races and we get the winners in each of the category. Let them be A1, B1, C1, D1, E1.
Now we conduct a race between the first horses from each of the 5 categories. The winner is the fastest.
To find the second fastest, we need to see which are all the eligible ones. Lets work on an elimination concept for this. The horses that could come second are - B1, A2, A3.
We eliminate C1,C2.., D1, D2.. E1, E2.. etc because B1 is faster than C1 and C1 is faster than C2, C3 etc. Similar concept applied to groups D and E.
The race among B1, A2, A3 would give the second fastest horse.
Total number of races = 7
For the second fastest horse, why you consider only B1,A2,A3?
We know A1 is faster than A2 and if A2 is faster than A3, then racing only B1 and A2 will suffice.
If we are not sure whether A2 is faster than A3, then we have to consider A4,A5 also.. Because we know only the facts that A1 is faster than A2,A3,A4,A5 and B1 is faster than B2,B3,B4,B5 and A1 is faster than B1. We can't determine which is faster among A2,A3,A4,A5 and B1
Correct me if I am wrong..
We dont have a stop watch here !!!!!!!!!!!
There is no way to determine that B1 is faster than C1 OR D1 OR E1. Without a stopwatch, in every race we have knowledge on the positions of the horse at the end of the race but not the speed.
So we cannot compare the horses across races directly. Only medium to compare is through a race :).
Can be done in only 4 races ....
Race [1]
Make 5 hourses run from one direction and another 5 from another direction, and also mark the middle point of the race...
Notice first 3 intersections of the hourses and select the hourses who crossed their first half mark as the 3 fastest...
Race [2] repeat this with another 10 hourses
Now we have 6 best hourses and 5 untested
Race [3] Make 4 hourses run from each direction and choose the 3 same as above
Now we have left with 6 hourses
Race [4] Make 3 hourses from each side run and choose the best 3....
Done in 4 races
@Sriram : Your first concern was actually valid. Your second comment is invalid because you cant note down the second fastest as you are not given a stopwatch.
I think of it as as function getMax with 5 parameters
function getMax(Horse []horse) // takes 5 horse parameters
{
//returns fastest horse
}
1. Creat 5 groups
2. call function 5 times for each 5 horse group
3. you have 5 fastest horse now
4. call function again with this 5 horses
5. you have the fastest horse now (total calls 5)
6. eliminate fastest horse and call function again with rest of 4 horses
7. you get the second fastest (total calls 5+1=6)
7 is not correct
Example:
In group one, out of 5 two horses have speed 50 and 60 units/hour.
And the highest speed in all other groups is 40. The race will be with horse with speed of 60 and 4 horses with speed of 40. 60 is the winner but the second fastest is not 40 but it is the horses with speed 50.
Correct solution is to eliminate fastest horse and take the second fastest from the group to which the fastest horse belong and make the five race.
Let {A1,B1,C1,D1,E1} be the winners of 5 groups {A,B,C,D,E}
and {A2,B2,C2,D2,E2} be the runner ups of 5 groups
This would take 5 races
6th race among {A1,B1,C1,D1,E1} would give us the fastest horse.
Let C1 be the runner up in this race.
Now the eligible candidates for runner up position are {A2,B2,C2,D2,E2} and C1 as well
We conduct 7th race between {A2,B2,C2,D2,E2}
Let D2 be the winner of 7th race
Now we make C1 and D2 run in 8th race.
The winner of 8th race is the 2nd fastest of all.
Please suggest if there is any other better alternate...
Ans-7
Divide the horses into 5 groups(A,B,C,D,E).Now find the fastest and second fastest in each group(A1,A2,B1,B2,C1,C2,D1,D2,E1,E2).Now find the fastest among all by the race among top 5 fastest(A1,B1,C1,D1,E1).We can get the fastest from here.Now for the second
fastest conduct a race between the winner's group 2nd fastest and all other group fastest.winner of this race would be the second fastest horse.
like-suppose winner is C1 the conduct a race between A1,B1,C2,D1,E1.so winner of this race is second fastest.
7 Trials is the correct answer I guess!
*Split the 25 horses into 5 groups a,b,c,d,e.
*Note down the top 2 positions in each group. Let them be a1,a2,b1,b2 etc.
*Now we know that the fastest horse is one of these <a1,b1,c1,d1,e1>
*To find the second fastest. Pick the second fastest horse from the group, from which the first horse was picked. i.e
- If a1 was picked then pick a2. If b1 then b2
*Now test this horse against the fastest horses of the other groups. i.e if a1 was picked then conduct a race between a2,b1,c1,d1 and e1. Else if b1 was picked then conduct a race between a1,b2,c1,d1 and e1
This is my solution : 8 Rounds
I will split 25 horses into 5 groups, each group having 5 horses. For each group, i will track 1st and 2nd fastest horses. This will take 5 rounds.
I will arrange a race for first fastest horses in the 6th round. I get the fastest horse among all 25 horses. But i will track the second fastest horse in this round as well.
In 7th round, there will be a race of all second fastest horses. I will track the fastest horse here.
In 8th round, there will be a race between 6th second fastest and 7th fastest horse. Fastest horse here would be the second fastest among all the 25 horses.
Why 8 rounds...
just make 5 groups from 25 horses.
and race them ...there will be 5 winners ...from each group
That will ensure that we have the best from all the 5 lots.
Now 6th race for the best among the best ..and also we will get the fastest as well as the second fastest...ie the number 1 and number 2 posistion holder...
GS : You are missing a scenario. For example, second fastest horse in the first group may run faster than the fastest horses among all other groups.
trial 1 : a1>b1>c1>d1>e1
trial 2 : a2>b2>c2>d2>e2
trial 3 : a3>b3>c3>d3>e3
trial 4 : a4>b4>c4>d4>e4
trial 5 : a5>b5>c5>d5>e5
trial 6 : a1>a2>a3>a4>a5
this means a1>a2 which is faster than b2 and [b2>c2>d2>e2] as per trial 2
a1>a3 which is greater than [b3>c3>d3>e3] as per trail 3
..........
so a2 is second fastest ..
What say ? Everyone :)
6 trials for fastest horse and 7 for second fastest
- Anonymous April 28, 2011