Google Interview Question: You have 25 horses, what is t... | Glassdoor

Interview Question

Linux Kernel Engineering Interview Mountain View, CA

You have 25 horses, what is the minimum number of races you

  can find the top 3. In one race you can race 5 horses, and you don't have a timer.
Tags:
brain teaser, logic, analytical, analytical reasoning
Answer

Interview Answer

31 Answers

1

I don't know, but Google it, you'll find the answer. Once you know it, it's obvious.

Interview Candidate on Mar 11, 2009
1

12

Anonymous on Apr 28, 2009
5

Probably 8.

First answer: at least 5 (you have to look at every horse).
Second answer: 11. You can race five horses, replace 4th and 5th place horse by two horses from pool of horses that haven't raced, and continue doing this until all horses have raced. Since there are 25 horses, you need 1 race for the first five horses and 10 to go through the remaining 20.
Third answer: 8.
Round 1 (5 heats) : Race horses in heats of five. Eliminate all 4th and 5th place horses. (15 horses left)
Round 2 (1 heat): Race winning horses from every heat. Eliminate 2nd and third place horses that lost to horses in round 2 that came third or worse. (7 horses left).
Round 3 (1 heat): Pick any five horses. Race them. Eliminate bottom two and replace with remaining two horses. (5 horses left)
Round 4 (1 heat): Eliminate bottom two.

Note:
after round 2 it may be possible to use some interesting decision tree mechanism to determine the three fastest horses using fewer horses per heat but you still need two heats to do that.
This problem assumes horses don't get tired, no ties are possible and a horse's speed is deterministic race after race after race. Good assumptions for brain teasers, but not for real life.

B on Apr 29, 2009
4

Actually, should be 7. Forgot that after round 2 you can eliminate one more horse leaving you with 6 horses, one of which (the winner of the second round), is the fastest horse of them all. One more race with the five horses not No.1 and you pick the top two.

B on Apr 29, 2009
8

6. First round consists of 5 races of 5 horses. Pull aside the winner of each race in the first round for the final heat and eliminate the bottom 2.

J on May 27, 2009
1

8.
Run 5 races of 5 horses. (5)
Put all the 3rd place horses in a race, winner is 3rd fastest. (6)
Put all the 2nd place horses in a race, winner is 2nd fastest. (7)
Put all the first place horses in a race, winner is fastest. (8)
The third place horse was beaten by one of the 2nd place horses. So third stays in third.
Same for the second place horse.

chzwiz on Jul 29, 2009
3

chzwiz, r u sure u r not high?
B, what happened in round 2? why 8 horses suddenly get eliminated by 1 race? again, do u know what you are smoking?

Anonymous on Aug 1, 2009
1

oh, forget J,..... just speachless

Anonymous on Aug 1, 2009
16

7. chzwiz, your answer is incorrect. One of the first or second place horses could be 2nd fastest and/or 3rd fastest. I drew a grid to visualize the problem. First, run five races to establish 5 groups of internally ranked horses, and you can of course immediately eliminate 4 & 5 of each race.

1 2 3 x x
1 2 3 x x
1 2 3 x x
1 2 3 x x

Then race 1st place horses, eliminate 4 & 5, and those they beat earlier. You can also eliminate the horses #3 beat, and the 3rd place horse from #2's first race.

1 ? ? X X
2 ? X X X
3 X X X X
X X X X X
X X X X X

You know #1 is fastest. Race the remaining horses (2, 3 and ?'s), and the top two are 2nd and 3rd. After reading the above answers, this is the same as B's revised answer, but I found it easier to explain/visualize with the grid.

Brad on Aug 17, 2009
0

@brad ... but isnt dat v dont have timer to know d ranking ... or my assumption is wrong??

I thought v know only 1 result of every race .. i.e. top 1

spawn on Feb 11, 2010
0

Spawn, it's true that you don't know timings, but you do know RANKINGS.

Jerry on Oct 9, 2010
0

I don't see errors in logic with each answer above. They may claim to discover the answer with fewer races, but they have too many assumptions or errors to be correct. To do this optimally and without any potential error, you must eliminate as many correctly placed horses as possible and eliminate the maximum number of non-win/place/show horses each round.

Round 1:
(Races 1-5) You must start with 5 heats @ 5 horses apiece. The non-Win/Place/Show horses of each round have been eliminated, leaving 15 horses.
Round 2:
(Race 6) You then race the top finishing horses against one another, leaving you with your overall fastest horse. We now have the Win horse, leaving only Place and Show as unknowns. This also eliminates the bottom two horses, leaving 2 horses from this race. There are also only two more unknown horses.
Race 7) You may race the Place horses against one another. Only two survive this race, since we know that each of these horses can place 2nd at best. This leaves 2 horses from this race.
(Race 8) You may also race the show horses of each round. Since they each have been beaten by two other horses in Round 1, only the 1st place horse of this round survives as the potentially 3rd best horse. This will leave 1 horse in this race to battle for the 2 remaining spots.

Round 2 leaves 2 Horses (Race 6) + 2 Horses (Race 7) + 1 Horse (Race 8) = 5 Horses, for 2 unknown spots.

Round 3:
(Race 9) We can now have the final 5 horses race for the remaining two spots leaving us with the undisputed 2nd and 3rd places.

Here is a more complex scenario below in which the top 2 horses come from the same original race and the later races are full of lame horses. Many of the solutions above do not address those problems. Each # is a horse, named by it's inherent ranking. X signifies 'eliminated horse'

Race1 R2 R3 R4 R5
1-->R6 3-->R6 11-->R6 16-->R6 21-->R6
2-->R7 7-->R7 12-->R7 17-->R7 22-->R7
4-->R8 8-->R8 13-->R8 18-->R8 23-->R8
5X 9X 14X 19X 24X
6X 10X 15X 20X 25X

Round 2:
Race 6 (Winners) Race 7 (Places) Race 8
1 -->1st Place 2-->Race 9 4-->Race 9
3 -->Race 9 7-->Race 9 8X
11-->Race 9 12X 13X
16X 17X 18X
21X 22X 23X

Round 3:
Race 9
2P
3S
4X
7X
11X

First Place = 1 Horse -- Race 6 Winner
Second Place = 2 Horse -- Race 9 Winner
Third Place = 3 Horse -- Race 9 2nd place

Josh E on Dec 30, 2010
0

To fix the formatting issues that appeared as spaces are eliminated above--

Race1________R2___________R3___________R4___________R5____

_1-->R6_______3-->R6_______11-->R6_______16-->R6_______21-->R6
_2-->R7_______7-->R7_______12-->R7_______17-->R7_______22-->R7
_4-->R8_______8-->R8_______13-->R8_______18-->R8_______23-->R8
_5X__________9X___________14X__________19X___________24X
_6X__________10X__________15X__________20X___________25X

Round 2:
Race 6 (Winners)__________Race 7 (Places)__________ Race 8

1 -->1st Place_____________2-->Race 9______________4-->Race 9
3 -->Race 9_______________7-->Race 9 _____________ 8X
11-->Race 9______________12X____________________13X
16X_____________________17X____________________18X
21X_____________________22X____________________23X

Round 3:
Race 9
2P
3S
4X
7X
11X

Less ascetically pleasing than the original, but it will suffice.

Josh E on Dec 30, 2010
0

This problem can be reduced to (viewed as) multiway mergesort:
1) split the horses into groups of five as if we had five files to sort
2) do a multiway mergesort but instead of sorting all horses, stop as soon as you get the top 3

We need one race per group to sort them (5 races total). And 3 more races to get the top 3. We will need 8 races.

The interviewer might have been looking whether an applicant was able to spot this relation to algorithms or not.

But if this was a brain teaser instead, maybe the answer is less than 8.

russell on Apr 2, 2011
9

The answer is definitely 7, here is a fantastic explanation:

http://www.programmerinterview.com/index.php/puzzles/25-horses-3-fastest-5-races-puzzle/

Maya on May 26, 2011
0

Randomly race the horses 5 times and keep a winner's bracket.

The 6th race will determine the fastest horse. This is trivial.

The 2nd and 3rd fastest could be in the winner's bracket or they could be the 2nd or even 3rd fastest in one of the losing brackets. Then we have to race 2nd and 3rd place from the winners bracket as well as from each original bracket, which had 5. This means we have 12 horses to try.

Randomly keep 2 horses out and race 5v5. Keep the top two horses from both. We have 6 horses now. Race 3v3 and keep the top two, leaving 4 horses. Then race these.

I count 11?

Anonymous on Feb 7, 2013
0

Brute force divide and conquer solution = 18 races.

Just be aware of the brute force solution in case there's restriction where we can't use the memory to save the first 3 places.

Leo on Dec 2, 2013
1

Those who want a system that averages that fastest solving time, the answer will be 7 races.. Those gamblers who want an algorithm that gives the fastest possible solving time (all though sometimes it WILL take longer) the answer is 6.

Race 1-5:
First 5 races of five.. We randomly seed with no overlaps.. we use this to eliminate the bottom two from each race.

I am going to use a two digit number labeling system where the first digit is which race in heat one they competed in and the second digit is which place they obtained in that heat..

These are the horses we have left:
11 12 13
21 22 23
31 32 33
41 42 43
51 52 53

Race 6:
We have the winners from each heat compete, and for ease of explanation lets say that they ranked 11, 21, 31, 41, 51.. Now we know that 41, 51 and all the horses slower than them (their heats) are out.. also, we know that any horse slower than 31 is out (ie. 32 & 33), and we know that any horse that is at least two horses slower than 21 is out (ie 23). This leaves 11, 12, 13, 21, 22, 31.. 6 horses.

Possible rankings:
11 12 13
21 22 23
31 32 33

Race 7
Rank 1 is already known.. and we just need to know how to organize 12, 13, 21, 22, and 31.. And look at that!! that's 5 horses! We race those 5 and the top two receive the overall rank of 2nd and 3rd.

5 races in round one, 1 race in round 2, 1 race in round 3.. it took us 7 races..

Option two: 6 Races
Now, an alternate answer.. when we read "What is the minimum number of races to find the top 3 horses" we can interpret that maybe they don't care about this method working every time.. instead they want to hit up the Vegas Casinos and test their luck, and maybe save some track time..

If i was a gambling man.. i would race the first group of those that i thought looked the strongest.. I would eliminate 2 in that race (we are now down to 23 horses).. and then race the 3rd fastest multiple times and hope that he gets first in the remaining races, thus eliminating 4 horses each time. This is going to be painful for the horse that placed 3rd in heat one.

Race 1: Eliminate 2, 23 horses remaining

Race 2.. Eliminate 4, 19 horses remaining

Race 3.. 15 horses

Race 4.. Eliminate 4, 11 horses

Race 5.. Eliminate 4, 7 horses

Race 6.. Eliminate 4, 3 horses.. The original top 3 from Race 1.

So using method 2, we completed this race in 6 races instead of 7!

Steve on Jan 16, 2014
0

I messed up the possible ranks after race 6.. That should have read..

11 12 13
11 12 21
11 21 22
11 21 31

Please forgive me.

Steve on Jan 16, 2014
0

Brad's explanation is the perfect one. All other explanations are junk and impossible to read and follow.

Abhi on Apr 20, 2014
0

Round1: (5 groups) remaining horse 15
Round: (1 groups). Topper from each heat
Round: (1 gounds). Forget about the 4th and 5th Heat horses (becoz their toppers are at last place)
go to the 3rd position

Vikas & Avineesh on Jul 24, 2014
0

6 races as the 25 horses are divided into 5 groups each grouping 5 horses in each group , then the winner from each group would be 5 then there should be last n final race to find the first , second and third placed horse . in this way we can find our top 3 horses :)

palak on Sep 27, 2014
1

It should be 11. In each race you can only eliminate 4th and 5th place simply because there's a chance that the top 3 in that single heat are the 3 fastest of all the horses. So each race you can select 5 random horses each race or keep the top three in the following race, the outcome is the same.

Ryan on Oct 16, 2014
0

I get 8 races to guarantee you get the top three.

5 races of 5 to get the fastest horse from each group.
1 race to determine the fastest overall.

The second fastest horse can be in the group (x) that produced the fastest,
1 race with the remaining 4 fastest from the other groups and the second fastest from group x.
That race will produce the second fastest overall guaranteed.

1 more race with the 3 or 4 from the original winners and the replacement from the group that produced the second place horse.

That makes 8 races.

Andrew on Mar 11, 2015
0

I dont get it.. It shoudl be five races right? U will know how long each horse takes to cover same distance. Pick the three that took shortest. Y need more rounds? If u down vote my answer, u have the obligation to explain why 😜

Anonymous on May 4, 2015
0

Answer is 6. You will have 5 races and 5 winners. Next race with 5 winners is the 6th race and you get top 3.
You're welcome.

Shah on Jun 11, 2015
2

It would be 7, mainly because since there only 5 racers each round, then it would be tournament style. As you can tell, tournament style never determines the rankings in the short term. (Imagine if a new tennis player played against Roger Federer and he loses. He's not bad, he's just really unlucky haha)

So in order to determine the answer, you would have to use massive logic and reasoning.

First step is obvious: you get 5 sets of horses to race through 5 races. So the number is already at 5. We get the top horse in each one and note them.

However, one problem is that you have to consider if fastest horses could lie all in the first, second, third, fourth, or fifth group. But we can't determine that yet, so let's race one more time with the winners of each race.

Second step: Race with each winner from their groups. That makes 6 total races so far.

Ok, now we can get somewhere with this. So now, its a question of which horses we can eliminate to determine the next race. Who can we eliminate though? Well, we can get rid of:

- All the horses in the group with the last race that were 4th and 5th place. This makes sense since if the winner of their groups only made 4th and 5th place in the tournament style race, then they are definitely not the fastest horses. 15 horses remaining.

- Well, we can also get rid of the last two horses of each group since we are only looking for the top 3. 9 horses remaining.

- We also can get rid of the first place winner since we already know he is definitely the best. 8 horses remaining.

- Since we already determined who the fastest horse is, now we have to determine who the second and third fastest horse can be. In that case, the second place horse from the last round can get rid of the third horse of that group, since he definitely has no shot of being the first or second fastest horse of this new race. Also, in the third group, we can get rid of the second and third horse in the group due to the same reasoning above. 5 horses remaining.

Oh wow! We now have 5 horses left! This means we can just have a clean race and find the second and third fastest horse.

So overall, the answer is 7 races!

Jonathan Woo on Sep 10, 2015
0

7

stae on Jan 13, 2016
0

In my opinion, you need 26 RACES to make sure that you get the top 3 without a timer.

You need to know that without a timer if we plan only 5 races with 5 horses on each one, the last horse on the first group could end the race in 1 minute while the first horse on the second group (the winner) could end it in 2 minutes, which mean that the last horse of the first group is much faster than the first horse of the second group..and this is why we need to make a total of 26 RACES to determine the top 3 as follow:

5 RACES with 5 horses each .> We get the 5 winners from each race .> We race the 5 winners > We get the top 4 > We race the top 4 with one of the remaining 20 horses > we get the top 4 > we race the top 4 with one of the remaining 19 horses > we get the top 4 > we race the top 4 with one of the remaining 18 horses...etc until the last race then we select the top 3 winners from the last race.

5 RACES + 1 RACE + 20 RACES = 26 RACES

Imad on Sep 25, 2017
0

HERE IS THE METHOD FOR THE MAXIMUM RACES😂😂
5 races 5 horses in a race, then repeat so you have a larger pool of data and more accuracy. Then take all the 1 and 2nd place winners that changed horses and run them 1 on 1 which ever wins takes the spot (0-20 races), so now you have 10 horses left. Split them into 2 groups and race each group twice repeating the 1 on 1 method using the first and second place horses, now you have 4 horses race them all in one race, run it twice again doing the 1 on 1 method. So that totals to 31 races...I think...also would be about time to retire these horses after 31 races LOL

JK on Feb 14, 2019
0

Shoot 36 races not 31

JK on Feb 14, 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.