# Trains and Birds

Question: A train leaves City X for City Y at 15 mph. At the very same time, a train leaves City Y for City X at 20 mph on the same track. At the same moment, a bird leaves the City X train station and flies towards the City Y train station at 25 mph. When the bird reaches the train from City Y, it immediately reverses direction. It then continues to fly at the same speed towards the train from City X, when it reverses its direction again, and so forth. The bird continues to do this until the trains collide. How far would the bird have traveled in the meantime?

Answer: Yes, you read it right. The bird is actually the fastest moving object in the problem!

Knowing that the bird is the faster than both the trains, you would only imagine that theoretically, the bird could fly an infinite number of times between the trains before they collide. This is because you know that no matter how close the trains get, the bird will always complete its trip before the crash happens. At the time of the crash, the bird would probably get squashed between the trains!

I bet sometime in school, you learnt how to sum up an infinite series. But do we have to do that?

The concept of relative speed (rings a bell?) can work handy here. Let’s assume that the distance between City X and City Y is d miles. The trains are approaching each other at a relative speed of (20 + 15) = 35 mph. The sum of the distances covered by the trains when they collide is d (i.e. the distance between the cities). Since distance/speed gives us time, we know that the trains collide d/35 hours after they start.

Since the speed of the bird is constant at 25 mph, we know that the bird would have covered

25 * (d/35) miles = 5d/7 miles

before the trains collide.

Simple question right? Let us know if you have an even simpler solution through our comments section!

If you're looking for some serious preparation for your interviews, I'd recommend this book written by a lead Google interviewer. It has 189 programming questions and solutions:

## 5 Responses

1. A2theYo says:

abhinav, I don’t think the relative speed of the bird matters. If we know how long the bird has been flying (d/35 hours) then we can calculate it’s total flight distance from it’s rate (25 mph).

2. abhinav says:

it is wrong solution as while going towards Y bird has speed of (25-15)i.e 10
and
towards X it has speed of (25+15)i.e 40

you also have to change speed of bird when you are in frame of X

3. thanks says:

Really thanks for the solution.
It helped me a lot in my campus placement.

4. Venkat says:

I bet there cant be more simplicity than this

5. Gaurav says:

This question has been a classic one for engineering entrance exams…Is it really for Microsoft?

XHTML: These are some of the tags you can use: `<a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>`