Skip to the guide

Geospatial indexing: geohash, quadtrees and S2

IntermediateConcept3 pages

"Restaurants within 500 metres", "the nearest drivers": every location feature searches by distance among millions of points. Geospatial indexes turn places on a map into keys an ordinary index can search. Interviewers expect you to pick one, choose a cell size from the search radius, and handle the edge of a cell.

The city map book

A printed city map book splits the city into pages, and each page into squares with names like "page 42, D4". To find cafés near you, you look up your square and search it. But the café two streets away may sit just over the line in D5, so you check the squares around yours too. The busy city centre gets extra pages at a larger scale, while one page of countryside covers several villages.

  • you, looking things up → the search service, and the cafés → the places it indexes
  • the squares → cells: areas with a short name that works as a key
  • your square's name → a geohash or cell ID: nearby places usually share most of it
  • checking the squares around yours → searching the neighbouring cells
  • extra pages for the centre → cells that adapt to density, as in a quadtree