Quick revision
The short answer
Geohash turns a location into a string where a shared prefix means a shared area, but close points can sit across a cell edge, so searches also read the eight neighbours, and its fixed cell sizes suit dense cities badly. A quadtree splits crowded cells into four, adapting to density at the cost of an in-memory tree to maintain. S2 covers any circle with a few cells of mixed sizes along a Hilbert curve, H3 uses evenly spaced hexagons, and all of them finish with an exact distance check.
What to remember
In this answer, we saw that all spatial indexes turn locations into cell ids so that ordinary sorted lookups can answer area queries. Geohash is simple and works anywhere but needs the eight neighbours for the edge problem and copes badly with uneven density. Quadtrees split where places are dense and keep work per search predictable, at the cost of maintaining an in-memory tree. S2 covers any shape with cells of mixed levels along a Hilbert curve, and H3 uses even hexagons that suit aggregation. Each of them ends with an exact distance check on the candidates.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Geohash turns a location into a string where a shared prefix means a shared area, but close points can sit across a cell edge, so searches also read the eight neighbours, and its fixed cell sizes suit dense cities badly. A quadtree splits crowded cells into four, adapting to density at the cost of an in-memory tree to maintain. S2 covers any circle with a few cells of mixed sizes along a Hilbert curve, H3 uses evenly spaced hexagons, and all of them finish with an exact distance check.
Why this question is asked
Every location product has to choose a spatial index, and the choice shows up in bugs: a cafe 50 metres away that never appears because it sits across a cell line, or searches that are fast in the suburbs and slow downtown. Interviewers ask this to see whether we understand why the schemes differ rather than only their names. In this answer, we will recap what a spatial index has to do, walk through geohash and its edge problem, see how fixed cells struggle with density, look at how quadtrees and S2 or H3 cells solve that, and finish with the trade-off every covering makes.
1A Quick Recap: Turning Two Dimensions Into Keys
Databases and search engines are very good at one kind of index: values in sorted order, where we can find a key or a range of keys quickly. A location is two numbers, latitude and longitude. If we index only latitude, a query for places near Berlin returns everything on that latitude band across the whole planet. So every spatial index does the same trick: it cuts the world into cells and gives each cell an id, so that a two-dimensional question becomes a few one-dimensional lookups.
A good scheme has two properties: nearby cells get nearby ids (locality), and a search circle can be described with few cells. The schemes below differ in how well they meet both.
2Geohash and the Cell-Edge Problem
Geohash alternately takes one bit from the longitude and one from the latitude, each bit saying which half of the remaining range the point is in, and writes the interleaved bits in base 32. Every extra character divides the cell into 32 smaller ones. The result is a string where a shared prefix means a shared area: every point inside the cell u33d starts with u33d. That makes it easy to use in any sorted store, from PostgreSQL B-tree indexes to Redis sorted sets, with a prefix or range scan.
But there is a catch, shown in the second pair. Two places 10 m apart can sit on opposite sides of a cell boundary, and their strings can differ early, so a prefix scan of one cell misses the other. The standard fix is to always read the cell containing the point plus its eight neighbours. That is why geohash searches read 9 cells rather than 1. The exact list of neighbour strings depends on the encoder, so the values above are illustrative; check your library.
3Why Fixed Cells Struggle With Density
Geohash cells at one precision are all roughly the same size. Places are not spread evenly. A precision-6 cell in central Tokyo can hold several thousand places, and one in the Australian outback holds none. If we pick a small precision to keep city cells small, a rural search reads nine empty cells and finds nothing; if we pick a large precision, a city search reads tens of thousands of candidates.
The usual answer is to search step by step: if too few results come back, drop one character of precision and retry, widening the area about 32 times. It works, but costs extra round trips in sparse areas and still overworks the densest cells.
4Quadtrees: Cells That Split Where Places Are
A quadtree starts with one cell for the whole map. Whenever a cell holds more than a limit, for example 100 places, it splits into four equal quadrants, and the places move into them. This repeats until every leaf cell holds at most 100 places. The result follows the data: downtown Manhattan ends up with leaf cells a few hundred metres wide, and the open ocean stays one huge leaf.
A search walks down the tree to the leaf containing the point, then collects neighbouring leaves until it has enough candidates or has covered the radius. Because each leaf holds at most 100 places, the work per search is predictable everywhere. The cost is operational. A quadtree is usually kept in memory on each search server, built at startup from the places table, and updated as places are added or removed, which means splitting or merging nodes. With 200 million places and 100 per leaf, that is about 2 million leaves, a few gigabytes with the ids, so it fits on one machine; many teams simply rebuild it periodically.
5S2 and H3: Hierarchical Cells on the Sphere
Geohash and a plain quadtree work on a flat latitude and longitude grid, so cells get thin and distorted near the poles. S2, a library from Google, projects the sphere onto the six faces of a cube and subdivides each face like a quadtree, with 31 levels from a whole face down to about a square centimetre. The cells are numbered along a Hilbert curve, a line that winds through the space without jumping, so cells whose ids are close are almost always close on the map. Each id is a 64-bit integer.
The strongest feature of S2 is the region coverer. Given a circle or any polygon, it returns a small set of cells, of mixed levels, that together cover the shape: large cells for the middle, small cells along the edge. The edge problem disappears because the covering is computed from the shape itself, not from the cell the point happens to be in. Density is handled by choosing the levels per query, or by storing each place at several levels.
H3, from Uber, covers the sphere with hexagons at 16 resolutions. A hexagon's six neighbours are all at the same distance from its centre, unlike a square whose diagonal neighbours are farther than its edge neighbours. That makes H3 excellent for aggregation, such as counting drivers per area. The catch is that hexagons do not nest exactly: a parent only approximately contains its seven children.
| Aspect | Geohash | Quadtree | S2 / H3 |
|---|---|---|---|
| Cell id | base-32 string, prefix = area | path in an in-memory tree | 64-bit integer per cell |
| Edge problem | read 8 neighbours | collect neighbouring leaves | cover the circle itself |
| Uneven density | fixed sizes, widen step by step | splits at a limit, adapts | mixed levels in one covering |
| Where it lives | any sorted store | usually in memory per server | any sorted store, via a library |
| Best at | simplicity, prefix scans | predictable work per search | accurate coverings (S2), aggregation (H3) |
6The Covering Trade-off and the Final Check
Every scheme approximates a circle with tiles, so every scheme reads some places outside the circle and must finish with an exact distance check, usually the haversine formula on the candidates. The design choice is how fine the covering should be. A few large cells mean few lookups but many extra candidates in the corners; many small cells hug the circle tightly but cost more lookups. S2's coverer exposes this as a maximum number of cells, often 8 to 20, tuned on real queries.
7Follow-up questions
Which one would you pick for a new nearby places search?
If the store is a general database or Redis and the team wants simplicity, geohash with the neighbour rule is fine. For a dedicated search service at scale, I would pick S2, because coverings handle the edge problem and density cleanly and the cell ids are compact integers. I would pick H3 when the main need is aggregation, such as heat maps or pricing zones.
Does Redis use any of these?
Yes. Redis GEOADD stores locations in a sorted set using a 52-bit geohash-style integer as the score, and GEOSEARCH reads the centre and neighbouring ranges and then filters by exact distance, which is the geohash approach with the edge fix built in.
8Conclusion
In this answer, we saw that all spatial indexes turn locations into cell ids so that ordinary sorted lookups can answer area queries. Geohash is simple and works anywhere but needs the eight neighbours for the edge problem and copes badly with uneven density. Quadtrees split where places are dense and keep work per search predictable, at the cost of maintaining an in-memory tree. S2 covers any shape with cells of mixed levels along a Hilbert curve, and H3 uses even hexagons that suit aggregation. Each of them ends with an exact distance check on the candidates.
Open with
All of these turn two-dimensional locations into cell ids so I can use one-dimensional lookups. Geohash interleaves latitude and longitude bits into a base-32 string, so a shared prefix means the same area and I can prefix-scan in any sorted store.
Then, if they want more
The problem is cell edges: two points ten metres apart can have different prefixes, so every search reads the centre cell plus its eight neighbours. Geohash cells are also a fixed size, so a city cell has thousands of places and a rural one none, and I end up widening the search step by step.
A quadtree fixes density by splitting a cell into four once it holds more than, say, 100 places, so work per search is predictable, but I have to keep the tree in memory and maintain or rebuild it.
S2 projects the sphere onto cube faces and numbers cells along a Hilbert curve, so ids keep locality, and its coverer describes any circle with a few cells of mixed levels, which handles both edges and density. H3 uses hexagons with equal neighbour distances, which I would use for aggregation.
Whatever I choose, I finish with an exact distance check, and I tune the covering between number of cells and extra candidates.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare geohash, quadtree and S2 or H3 cells for nearby search, explaining the cell-edge...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/124c7b1a7d/