## How to design a map function to find the nearest gas station?

The 080th letter in Wu Jun's "Letter from Silicon Valley" talks about a Google interview question.

The topic is like the title, and it mainly examines two aspects:

1. To examine the basic knowledge of computer science

2. Look at the candidate's ability to decompose and solve problems

To deal with a problem first, you must first fully understand the problem, otherwise the answer will not be what you asked or you will not understand the point of view of the person who asked the question. I just started thinking about this question and missed a key point:

1. The car is moving, the results will be updated continuously, and the calculation speed should not be very slow.

2. Different usage scenarios of this product have different acceptance of accuracy. If the difference between two gas stations is 200 meters, drivers and pedestrians have different feelings about 200 meters.

3. Cars have directions and move. The car location only has GPS positioning, which needs to be converted into a range of street addresses, etc.

4. Calculation of distance. In reality, the distance between two points is not a Euclidean straight-line distance, and it is impossible for a vehicle to go straight through the building. The distance between two points is the superposition of many distance segments, and there are N combinations of lines for each distance between two points. How to find the shortest one? The shortest path can be found using a dynamic rule method.

After finding a list of all gas station distance results, sort all gas stations by distance. A few other things to consider:

1. Sorting complexity. There are 1000(n) gas stations in Beijing, and the algorithm complexity of ordinary sorting is n*log(n).

2. Calculation magnitude. Consider that the car is moving and the data is updated 3 to 5 times per minute. There are millions of cars on the road in Beijing, and maybe thousands looking for gas stations.

3. Narrow down the scope of the problem. Starting from the problem, I learned that as long as the nearest gas stations are found, users of gas stations that are far away do not care. So there is no need to sort so many gas station distance results. When it comes to a special category of binary weights, namely "heap", this data structure can only sort out the first few, regardless of the latter. This algorithm is also called small-scale heap sort.

Note: Using this algorithm, the complexity of calculating the first place is N, and the computational complexity of the second and third places are Log(n). If you only need to find the nearest 10 gas stations, the magnitude of the calculation is about 1000 (1000+x*10), and this algorithm is also called the TopN algorithm.

Combined with real scenes, continuous optimization:

Pre-calculation + global optimization: Calculate the point-to-point distances between all intersections in Beijing in advance. When a person wants to find a gas station, the distance calculation becomes the distance from the current position to several nearby intersections. Then calculate the distance from a gas station to the intersection near its location. Since the distance from each intersection point to point is calculated in advance, the result can be calculated by doing several additions.

The author summarizes several thinking inspirations that this question may bring:

1. Don't do useless work.

2. Many things follow the same rule.

It is very important to learn the theory (algorithm), and at the same time to understand why the theory is proposed, what application problems it solves, and then it is all right.

3. When solving problems, try to avoid subjective assumptions.

For the gas station question, we have assumed for a long time that the search for a gas station is for my own personal service, and we did not consider it. This assumption is not in the question. If we can't get out of our limitations, we can't optimize further.

### Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169317&siteId=291194637
Recommended
Ranking
Daily