Let’s say that you have 25 horses, and you want to pick the fastest 3 horses out of those 25. In each race, only 5 horses can run at the same time because there are only 5 tracks. What is the minimum number of races required to find the 3 fastest horses without using a stopwatch?

This question is an interesting one. Let’s start by asking ourselves, what are our limitations? Well, we don’t have a stopwatch so we can’t time the horses. That means that we can’t compare the race times of each horse – otherwise we could simply have 5 races of 5 horse each and pick out the 3 fastest times to get the 3 fastest horses. Remember that we can only race 5 horses in each race (otherwise we could just have one race with 25 horses, pick the 3 fastest, and we would be done!).



Draw out a table

In problems like this, it helps tremendously to create some sort of visual aid that you can refer to. With that in mind, we have created this table where each entry represents a different horse.

X1   X2   X3   X4   X5 
X6   X7   X8   X9   X10 
X11 X12 X13 X14 X15 
X16 X17 X18 X19 X20 
X21 X22 X23 X24 X25 

Let’s say that we have 5 races of 5 horses each, so each row in the table above represents a race. So, “X1 X2 X3 X4 X5 ” represents a race, and “X6 X7 X8 X9 X10 ” represents another race, etc. In each row, the fastest horses are listed in descending order, from the fastest (extreme left) to the slowest (extreme right). The fastest horses in each race are the ones on the left – so in the first race X1 was the fastest and X5 was the slowest. In the second race X6 was the fastest, X7 was the second fastest and so on.

Only 5 horses each race

So, now we ask ourselves: what do we know after these 5 races? Well, we do have the 5 five fastest horses from each race (X1, X6, X11, X16, and X21). But, does that mean we have the 5 fastest horses? Think about that for a second. Well, actually it does not mean that we have the 5 fastest horses. Because, what if the 5 fastest horses just happened to be in the first race – so X1 X2 X3 X4 X5 are the fastest horses. X1, X6, X11, X16, and X21 are all the fastest horses in their individual groups, but there could be one group that just happened to have all of the fastest horses. Remember we haven’t compared all the horses to each other since we can only run 5 horses in a race, so that is still a possibility. This is very important to understand in this problem.

Work through a process of elimination

Well, now that we’ve had 5 different races, we can eliminate the slowest 2 horses in each group since those horses are definitely not in the top 3. This would leave these horses:

X1   X2   X3   
X6   X7   X8   
X11 X12 X13 
X16 X17 X18  
X21 X22 X23 

We also know the 5 fastest horses from each group – but it’s important to remember that the 5 group leaders are not necessarily the 5 fastest horses. So what can we do with that information? Well, we can race those 5 horses against each other (X1, X6, X11, X16, and X21) and that would be the 6th race. Let’s say that the 3 fastest in that group are X1, X6, and X11 – automatically we can eliminate X16 and X21 since those 2 are definitely not in the top 3.

What other horses can we eliminate after this 6th race? Well, we can automatically eliminate all the horses that X16 and X21 competed against in the preliminary races – since X16 and X21 are not in the top 3 then we also know that any horse that’s slower than those 2 is definitely not in the top 3 either. This means we can eliminate X17 X18 X22 and X23 along with X16 and X21.

Now, we also know that X1 is the fastest horse in the group since he was the fastest horse out of the 5 group leaders. So, we don’t need to race X1 anymore. Are there any other horses that we can eliminate from further races? Well, actually there are. Think about it – if X6 and X11 are the 2nd and 3rd fastest in the group leaders, then we should be able to eliminate X8 since X6 raced against him and he was in 3rd place in that race. X7 could only possibly be the 3rd fastest, and since X8 is slower than X7, we can safely eliminate X8. We can also eliminate X12 and X13 since X11 was the 3rd fastest in the group leaders, and X12 and X13 were slower than X11. So, all together we can eliminate these horses after the 6th race: X17 X18 X22 X23 X16 X21, X12, X13, X8 and X1. This leaves us with the following horses to determine the 2nd and 3rd fastest horses:

X2   X3   
X6   X7   
X11 

What is the solution?

This means we only have 5 horses left! Now we race those horses one more time – in the seventh (7th) race – and we can take out the top 2 horses and that would mean we have the 2nd and 3rd place horses! So, we have found our answer! It takes 7 races to find the top 3 horses in this problem.


Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.



FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon