educative.io

Educative

How to efficient get the neighboring 8 grids and why only 8 grids?

If the grid size is equal to the distance we want to query, then we only need to search within the grid which contains the given location and neighboring eight grids.

This part of the solution mentions that given a location, find its grid id and get 8 neighboring grids. However, I do not completely understand. I have the following questions.

  1. Given a location of a user, how do you get the corresponding grid id?
  2. How do you get the 8 neighboring grids?
  3. Why do we search only for 8 neighboring grids? What if the user is searching for nearby places within 100 miles? How many nearby grids should you inspect?
2 Likes