An interesting interview question, a question about birds and trains, again

Source: Internet
Author: User

First, let's talk about the context:

Http://www.cnblogs.com/beginor/archive/2009/04/27/1444844.html
Http://www.cnblogs.com/ilovemeyou2000/archive/2009/04/29/1445797.html

This "interesting" interview question is: there is a train between the two places 100 kilometers away, and a train leaves the ground for B at a rate of 15 kilometers per hour, another train departs from B to a location at a speed of 20 kilometers per hour. If there is a bird that starts at a speed of 30 kilometers per hour and two trains start at the same time, depart from the ground a, return after encountering another car, and fly back and forth between the two trains in turn, until two trains meet each other. Excuse me,How many times does this bird make a round trip??

First of all, I think there is nothing not rigorous about this question, and there is no obvious misunderstanding. According to the general expression, it is clear that the meaning of the question is to ignore the Turning time of the bird, ignore the length of the bird, and abstract the bird and the locomotive into three points. If I have to consider the Turning time and length of the bird, I think it is possible to misinterpret it intentionally. Any application question has an implicit ignore condition. It is impossible for the respondent to require unlimited and accurate expression of the application question. So I think there is no need to talk about quantum physics. If you want to drill the horns, you can also say that the train road is not a strict straight line, it can be said that the bird is not a constant linear motion, it can be said that according to the relativity speed and distance, time should follow the Lorenz transformation rather than Galileo transformation.

Then there is a solution. According to the original intention, the bird has been round-trip for an unlimited number of times. It can be proved by mathematical induction:

Assume that the k-th car is located at the location of the m bird
When flying to Car B, the two cars are separated.
M-m/(20 + 30) * (15 + 20) = 0.3 m
Then fly to chelaile
0.3 m-0.3 m/(15 + 30) * (15 + 20) = 1/15 m

The distance between the second round trip and the fifth round trip is m/15

Obviously, if m is greater than 0, the distance between the two vehicles after k + 1 is not 0.

At first, the distance between the two vehicles is 100 kilometers rather than 0. Therefore, no matter how many trips the two vehicles are between, the distance between them is not 0.

The answer to this question should have been answered.

However, many friends set a Distance Accuracy for programming. If the distance is smaller than the distance, the two cars are considered to be separated, and then the code implementation is written recursively or iteratively. But programmers are not coders, and not all codes that can be implemented are good. You cannot only know the simulation, regardless of the efficiency.

According to the previous proof, the final round-trip count is only a logarithm operation (note that the round-trip count is used instead of the round-trip count)

Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace bird
{
Class Program
{
Static void Main (string [] args)
{
Double exactDistance = 0.000000001;
Console. WriteLine (Convert. ToInt32 (Math. Ceiling (System. Math. Log (exactDistance/1000, 1.0/15 ))));
}
}
}

 

The last question about infinite time segments was actually raised and solved many years ago: http://en.wikipedia.org/wiki/Zeno's_paradoxes

In a similar time-based system, the number of times is not a paradox, and distance is.

Therefore, as an interview question, this question is a very good question. You can answer the question from different perspectives:

1. Basics of mathematics and logical thinking: Check whether the subject understands similar problems and background knowledge, and whether the subject can come up with a solution independently.

2. algorithm optimization Consciousness: Check whether the interviewer only pays attention to the most superficial solution. If you do not want to write recursive Code directly, the programmer must not.

3. ability to understand the problem: Check whether the interviewer can correctly understand the problem and actively communicate with others when there is a deviation to find out the true intention of others. I assume that programmers with a bunch of conditions must not

4. programming language basics: Check whether the interviewer can use a computer language correctly. Programmers who use = to directly compare floating point numbers must not

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.