Stage 3 · Distributed data · Medium · 15 min

Geospatial indexes: finding what is nearby

How an app finds the drivers, shops or friends near you without checking every place on the map: grid cells, geohash codes and quadtrees.

What each interview level is expected to know

Learn first: Indexes and storage engines, Replication and sharding

After this lesson you can:

  • Find nearby places by reading a cell and its neighbours, not the whole map.
  • Compare a fixed grid, geohash codes and quadtrees.
  • Choose a cell size, and keep moving drivers up to date.

Start here

See it in 60 seconds

Finding places nearby with a quadtree

A quadtree cuts the map into four squares, and cuts again only where places crowd, so a nearby search checks a few small squares.

Scene 1 of 6 · Find coffee near me

A user in the busy city centre asks for the coffee shops nearest to where they stand.

You, in the centreYou, in the centreQuadtree indexQuadtree index76 places on a 16 km mapYour squareYour squareSquares next doorSquares next doorCountryside squareCountryside square
  • You, in the centre — healthy
  • Quadtree index — healthy — 76 places on a 16 km map
  • Your square — healthy
  • Squares next door — healthy
  • Countryside square — healthy
Levels at the centre
not shown yet
Smallest square (m)
not shown yet
  1. Find coffee near me. A user in the busy city centre asks for the coffee shops nearest to where they stand.
  2. Split into four. The whole map holds more than 4 places, so it splits into four squares, each 8 km wide.
  3. Crowded squares split again. The square holding the busy centre splits again, and again, wherever more than 4 shops crowd.
  4. Quiet squares stay big. The south-west square has only 3 farms, so it stays one 8 km square, and a search there is quick.
  5. Check next door too. The search reads the user's small square, then squares next door until it has enough shops.
  6. Geohash, a related idea. Geohash is a fixed grid instead: each cell gets a code like 9q8y, and nearby cells usually share its start.
  7. Now you try. How many shops crowd into the 1 km city centre? For example: The centre goes 7 levels deep, down to squares 125 m wide, while the countryside stays one 8 km square. A square splits only when it holds more than 4 places, so the tree grows deep only where places crowd.

Scene 1 of 6: Find coffee near me. A user in the busy city centre asks for the coffee shops nearest to where they stand.

Key points

  1. A square with more than 4 places splits into four smaller squares.
  2. Crowded city squares split deep, while empty countryside stays one big square.
  3. A search also reads the squares next door, since a shop can sit across a line.
  4. Geohash is a related idea: a fixed grid where each cell gets a short code.

Go deeper in the Map cells: find what is nearby lab →

In plain words

A rider opens a ride app and asks for drivers within 1 km. The app could measure the distance to all 200,000 drivers in the city, but that is far too slow to do for every rider. A normal sorts by one value, and a place has two: how far east and how far north. A solves this by cutting the map into cells. Each cell keeps a list of what is inside it, so a search reads a few cells near the rider and skips the rest. A is a short code for one cell of a fixed grid. A uses big squares in empty places and small squares in crowded ones.

For the rider asking for drivers within 1 km, the search runs in three steps. First, it finds the cell the rider stands in. Then it adds the cells around it, because a driver 50 m away may sit across a cell edge. Last, it measures the real distance only to the drivers in those few cells, and drops any that are over 1 km. When a driver moves into a new cell, the app moves them to that cell's list.

The problem it solves

An index on "how far north" finds everyone in a thin band that runs right round the Earth. An index on "how far east" finds another band. Neither can answer "within 1 km of me" without checking a huge number of rows. And drivers move, so the index must also take thousands of updates a second.

Why it matters

"Find the drivers near me" is one of the most common interview problems, and food delivery, maps and dating apps ask the same question. A normal database index cannot answer it quickly, so you need to know what can.

A worked example

Say a city has 200,000 drivers, and a grid with cells 1 km wide has 10,000 cells. That is about 20 drivers in each cell. A search reads 9 cells, so it checks about 180 drivers, not 200,000. That is over 1,000 times less work.

Think of it like…

Think of a paper street atlas. To find a café near you, you open the page for your part of town, not every page. If you stand at the edge of the page, you also check the next one.

Where this analogy stops working

Looking only at nearby pages is the useful part. But an atlas is printed once. Drivers move every few seconds, so their cell lists change all the time. The index has to take thousands of updates a second, which is why it is often kept in memory.

Try it

See it working

Lab · Distributed data

Map cells: find what is nearby

A rider wants the drivers within a short distance, out of 200 on the map. Search three ways: measure every driver, read a fixed grid of cells, or read a quadtree. See which cells are read and how many drivers are checked.

Try it

033122123211300301213302303
DriverCheckedFound in the circleMissedRiderCell that was read
Cells read
9/ 64
the rider's cell and the ring round it
Drivers checked
158/ 200
distance measured to each one
Found in the circle
118
Healthy: within 1.0 km of the rider
Missed
0
none
Rider's cell code
300
cells with the same first digit share a quarter of the map

Numbers add up:118 drivers in the circle = 118 found + 0 missed · 158 of 200 drivers checkedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

How to search

Cut the map into 64 equal cells. Read only the cells near the rider. A geohash names cells like these.

Where the drivers are

Three drivers in four are in one crowded 2 km square.

The rider
9.0 km
9.0 km
1.0 km

Fixed grid only. Off: the search reads the rider's own 2 km cell and nothing else.

Tip: you can also click the map to move the rider.

Drivers checked, by method

  • Scan every driver200
  • Fixed grid158
  • Quadtree149

Every method finds the drivers it checks that are inside the circle. Read for this search: fixed grid 9 cells, quadtree 73 squares. A careful grid search would read 5 cells and check 155 drivers.

What this model simplifies
  • The map is flat and 16 km wide. Real indexes cover a round Earth, where cells get narrower away from the equator.
  • The fixed grid reads every cell in the ring, even corner cells that the circle does not touch. A careful grid search skips those.
  • Drivers stand still. Real drivers report a new place every few seconds, so their cell must be updated all the time.
  • The cell code has 3 digits, and each digit picks 1 of 4 parts. A real geohash character picks 1 of 32 parts, and 6 characters name a cell about 1.2 km by 600 m.
  • Reading a cell is free here. In a real system each cell read is a lookup in a database or a cache.
  • The quadtree is built once, and splits a square that holds more than 4 drivers. A real one holds far more per square, and splits and merges as drivers move.

Also try: Sharding

Mechanics

How it works

A geospatial index answers "what is near here?" by reading a few cells of a grid instead of measuring every place. The picture beside step 2 shows why the neighbour cells matter.

  1. Cut the map into cells

    Lay a grid over the map, with cells 2 km wide, for example. Store each driver under the cell they are in. To search, work out which cell the rider is in, and read that cell's list.

  2. Read the neighbour cells too

    Pick a case to follow below

    With this case: Reading one cell found D1 at 900 m and missed D3 at 300 m, which was in the next cell.

    North-west, 0 driversNorth-west0 driversNorth, 1 driverNorth1 driverNorth-east, 1 driverNorth-east1 driverWest, 0 driversWest0 driversRider's cell, 2 drivers, NOWRider's cell2 driversrider hereNOWEast, 1 driverEast1 driverSouth-west, 0 driversSouth-west0 driversSouth, 1 driverSouth1 driverSouth-east, 0 driversSouth-east0 drivers
    • North-west — 0 drivers — healthy
    • North — 1 driver — healthy
    • North-east — 1 driver — healthy
    • West — 0 drivers — healthy
    • Rider's cell — 2 drivers — healthy — NOW — rider here
    • East — 1 driver — healthy
    • South-west — 0 drivers — healthy
    • South — 1 driver — healthy
    • South-east — 0 drivers — healthy
    Step 1 of 3

    Step 1 of 3 · Rider's cell

    Done: Find the rider's cell

    What happens
    The rider stands in the middle cell, 200 m from its east edge. Each cell is 2 km wide.
    Why
    Working out the cell from a place is one quick sum, with no search.
    What changes
    The search will read this cell only.
    Next
    Read the rider's cell
    Cells read
    0
    the rider's cell only
    Drivers measured
    0
    Nearest driver found
    –
    not yet

    Step 1 of 3: Rider's cell. Find the rider's cell. The rider stands in the middle cell, 200 m from its east edge. Each cell is 2 km wide.

    What these pictures simplify. This picture uses 2 km cells and a few stated drivers. A real city has thousands of drivers per cell in busy areas, and cell sizes are chosen to match the search radius.

    A rider 200 m from the edge of a cell has nearby drivers in the next cell. So read the rider's cell and the 8 cells round it: 9 lookups. Then measure the real distance to each driver in those lists, and keep the ones inside the circle.

    In the picture, the rider stands 200 m from the east edge of their cell. Reading only that cell finds D1, 900 m away, and misses D3, which is 300 m away in the next cell.

    Reading the 9 cells finds D3. The real distance then decides who is inside the circle: a driver in a near cell can still be too far away.

  3. Give each cell a short code: geohash

    A geohash turns a place into a short string, such as tdr1y. Each extra character cuts a cell into 32 smaller ones. So 5 characters name a cell about 5 km wide, and 6 characters one about 1.2 km wide. Every place in a cell starts with that cell's code, so an ordinary index on the string finds them.

  4. Use a quadtree where places crowd

    One cell size is too big for a city centre and too small for a desert. A quadtree starts with one square and splits a square into four when it holds too many places, such as more than 4. Crowded areas end up with small squares, and empty areas stay as one big square.

  5. Keep moving things up to date

    A driver's phone sends its place every few seconds. Each update takes the driver out of the old cell's list and puts them in the new one. So moving things are often kept in memory, in a store such as . A driver who has sent nothing for about 30 seconds is removed.

  6. Split the index by area

    The cell code is a natural key: one city's cells can live on one shard. A search near the border between two shards asks both, the same way it reads neighbour cells.

Use it when

  • Any "near me" question: drivers, restaurants, friends, delivery riders.
  • A fixed grid or geohash when places are spread fairly evenly, or when your database only has ordinary indexes.
  • A quadtree when crowding differs a lot from place to place, such as a city and the land round it.

Skip it when

  • A handful of places, such as 50 shops: checking all of them is simpler, and fast enough.
  • Reading one cell only: it misses places that are across the cell's edge.
  • Trusting the first characters of a geohash to mean "nearest". Two places 100 m apart can sit on either side of a cell edge and share no starting characters.

Check yourself

Two places share the same first 6 characters of their geohash. What does that usually tell you?

Judgement

Trade-offs and how it fails

What you give up

Fixed-grid
Simple, and works with any database index, but one cell size cannot suit both a city centre and empty land.
Quadtree
Fits itself to crowding, but it is a tree to keep in memory and to change as places are added and removed.

Failure modes

  • A cell that is too big: one crowded cell holds most of the drivers, and every search there checks them all.
  • A cell that is too small: a wide search reads hundreds of cells, and most of them are empty.
  • Stale places: a driver who went offline still shows as nearby until the entry is removed.
  • A hot city on one shard: a concert sends all its traffic to the shard that owns that area.

In the real world

  • Redis stores places with GEOADD and searches them with GEOSEARCH. Inside, each place is kept as a geohash-style number, in a list sorted by that number. Redis cannot expire one place in that list, so old ones must be removed by their last-seen time.
  • Uber built H3, an open-source grid of six-sided cells, to group riders and drivers by area.
  • Google's S2 library cuts the globe into cells and numbers them, so cells that are close on the map usually have close numbers. Source: The S2 Geometry library (checked 14 October 2026)
  • PostGIS adds geospatial indexes to PostgreSQL. They are built on a tree of boxes drawn round groups of nearby places, called an R-tree.

Trade-offs to be ready to discuss: Fixed grid vs quadtree · Cell size large vs small

Check yourself

Why use a quadtree instead of a fixed grid for drivers in a city?

Interview

What interviewers look for

  • For "find nearby drivers", name the index first: "I'd store drivers by geohash cell, and read the rider's cell and its 8 neighbours."
  • Say how you pick the cell size: about as wide as the search radius, so 9 cells cover the circle.
  • Mention updates: drivers move, so the cell lists live in memory, and a driver who stops reporting is removed.

Say it out loud: Why does a nearby search read 9 cells and not 1?

A grid has cells 2 km wide. A search reads the rider's cell and its 8 neighbours. How far from the rider is it sure to cover, in every direction?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorUseful
Why a normal index cannot answer "near me" quickly, and the idea of cells.
MidExpected
Geohash cells, reading the neighbour cells, and how to pick a cell size.
SeniorExpected
A fixed grid against a quadtree, moving drivers, and splitting the index by area.
StaffUseful
Hot cities, borders between shards, and what stale places cost the product.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

A grid has cells 500 m wide. A rider wants every driver within 1 km. Is reading the rider's cell and its 8 neighbours enough?

Apply it

A quadtree splits a square into four when it holds more than 4 places. A square holds 4 drivers, and a fifth drives in. What happens?

Judge a design

A ride app keeps 200,000 driver places in its main SQL database, updating each row every 4 seconds. The database is struggling. What is the better design?

Next: CAP and PACELC, what a database can still promise when its machines cannot reach each other.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsRedis geospatialRedis documentationStoring points and searching by radius, built on a geohash inside a sorted set.
  • Official docsS2 GeometryS2 Geometry project (Google)Covering the sphere with cells so nearby places share a key prefix.
  • Official docsH3 documentationH3 project (Uber)Hexagon cells and why hexagons give every neighbour the same distance.