A: A Microsoft interview question discussed with the Weibo front-end instructor during dinner"

Source: Internet
Author: User

I don't know if it is a Microsoft interview question. The question is as follows:

 

A car is driving on an infinite long road. Its start position and speed per unit time are all finite integers (positive and negative uncertain). An instrument is available, you can check whether a vehicle is in a specified position within each time unit. You can find the speed and initial position of the vehicle within a limited period of time.

 

Answer:

There are two steps to solve this problem. The first step is to test the car once.

The second step is to find the vehicle's speed and displacement.

 

First, answer the first step,

 

Obviously, if the vehicle displacement is s and the speed is v, the vehicle location must be s + v * t at the time t.

 

Now, when the time is t, I can make a guess on the value of <s, v> <x, y>, and then test the position x + y * t. If there is a car at this position, the first step is to solve the problem. If there is no car in this location, it must be an incorrect guess. That is, the solution <x, y> can be ruled out.

 

So now we need to construct a sequence <x (t), y (t)> so that for any <s, v> value, there is always a limited t so that the <x (t) and y (t)> values are <s, v>, the geometric meaning of this problem is to map all vertices in the plane to a sequence. One of the most easily implemented solutions is given below. (I am talking about SPIRAL ing at dinner, but it seems that it is not as easy to program as a diamond ing)

If the sum of the absolute values of s and v is m, m must be limited. You can use the algorithm shown in the following code to detect it:

Var t = 0;
For (var m = 0; m <Infinity; m ++ ){

For (var x = 0; x <= m; x ++ ){
Var y = m-x;

If (check (x + y * (t ++ ))){
Break;
}
If (check (x + (-y) * (t ++ ))){
Break;
}
If (check (-x) + y * (t ++ ))){
Break;
}
If (check (-x) + (-y) * (t ++ ))){
Break;
}

}

}
// I know that you are sure you want to beat me after reading the code. That's right. The previous nonsense is actually a simple thing ......

Step 2,

Because the assumption in the First Step <x, y> is not a necessary condition for the location, it is impossible to determine that <s, v> must be <x, y> after the vehicle is hit. But after we know the current location of the car, the speed of the car can be x, according to,-3 ...... this sequence is guessed in sequence, and the second hit will get the speed v.

The initial position s can be calculated based on the speed.

 

 

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.