Requirements. Search businesses by location and a radius of 0.5 to 20 km, show their details, and let owners add and update them, with changes visible by the next day; keep search latency low through the lunch and dinner peaks, and never keep user locations longer than needed.
Searches
5,800 per second100M users × 5 a day, peak about 15,000
Businesses
about 200 GB200M × 1 KB
Geo index
about 3 GBfits in memory
The design. Map two dimensions to one with a geohash: each character narrows the cell, so nearby places share a prefix. A search takes the user’s cell and its eight neighbors, fetches the candidates from a geo index that fits in memory, computes exact distances, drops those outside the radius and sorts. A stateless location service reads from a geo cache and read replicas; owner edits go through a separate business service to the primary, and the index catches up by the next day.
Interview prompt
Design the backend for “restaurants near me” as in Yelp or Google Maps: given a location and a radius, return nearby businesses.
Requirements and estimates
Functional requirements
Search by location and radius: 0.5, 1, 2, 5 or 20 km.
View business details.
Owners add and update businesses; changes may appear the next day.
Non-functional requirements
Low search latency.
Handles lunch and dinner peaks.
User locations are private and not stored longer than needed.
Estimates
Searches 100M DAU × 5 searches ÷ 86,400 s ≈ 5,800 QPS, peak ≈ 15,000
Businesses 200M × 1 KB ≈ 200 GBGeo index 200M × (6 B geohash + 8 B ID) ≈ 3 GB, fits in memory
Whiteboard
What you sketch in the first five minutes: why one index on latitude does not help, then the nine cells every search reads.
1. Two strips, not one square
Say: an index on latitude finds a strip around the whole planet; we need one that finds a square.
An index on latitude returns everything in a thin band around the globe.
An index on longitude returns another band, and the database intersects two huge sets.
A geohash turns the square into one key prefix, so one index range finds it.
Next question: why not a quadtree? It adapts to density, small cells in Manhattan and huge ones in a desert, but it lives in memory and is harder to update live.
2. The cell and its eight neighbors
Say: two cafés across a street can be in different cells, so never search only one.
Find the user’s cell at a precision about the size of the radius.
Fetch the places in that cell and its eight neighbors.
Compute exact distances, drop those outside the radius, and sort.
Next question: too few results? Drop one character of precision, which makes every cell larger, and search again.
Why a plain index fails
SELECT id FROM business
WHERE lat BETWEEN 48.853 AND 48.863
AND lng BETWEEN 2.284 AND 2.305;
An index on latitude returns a thin strip around the whole planet, and one on longitude returns another. The database has to intersect two huge sets. What we need is a way to map two dimensions to one so that points close on the map stay close in the index.
Geohash does this. Split the world in half by longitude and write 1 if the point is in the upper half, 0 if lower. Then split by latitude, then longitude again, alternating. Every 5 bits become one base32 character, and each extra character makes the cell 32 times smaller. Nearby points usually share a prefix, so “everything in this cell” becomes a prefix lookup.
Explorer: encode a location
Geohash
Bits, 5 per character (blue longitude, red latitude)
First splits
Axis
Range
Midpoint
Bit
Cell size here
Suggested precision
The boundary problem
Two cafés across a street can sit in different cells with no shared prefix. So never search only the user’s cell: query the cell and its eight neighbors, compute exact distances for the candidates, drop those outside the radius and sort. If too few results come back, drop one character of precision and search again.
Index
Idea
Strength
Weakness
Geohash
Fixed grid, cell ID is a string prefix
Works in any database or Redis (GEOSEARCH); simple to shard and cache
Fixed cell size regardless of density
Quadtree
In memory tree; split any cell holding more than K places into four
Adapts to density: small cells in Manhattan, huge ones in a desert
Built at startup, harder to update live
S2 or H3
Hierarchical cells on a sphere (S2 squares on a Hilbert curve, Uber’s H3 hexagons)
Covers any shape well; good for geofences
More complex to explain and operate
Split reads from writes. The location service is stateless and scales out for 15,000 QPS. It looks up the nine cells in the geo cache, falls back to read replicas, then hydrates business details. Owner edits go through a separate low traffic service to the primary; a nightly job rebuilds the index. The 3 GB index is replicated to every read node rather than sharded.
What the interviewer probes next
What changes for moving objects, like ride share drivers?
Locations update every few seconds, so it becomes write heavy. Keep driver positions in memory (Redis geo sets or an in memory grid keyed by cell) with short TTLs, and never write every update to the main database.
Should we shard the geo index?
Not by default. At 3 GB it fits in memory on every replica, and sharding by region creates hot shards in big cities. Replicate it and shard only the business table, by business ID.
How do you cache searches when every user is at a different point?
Cache by cell, not by coordinates: key is the geohash at the precision for the radius, value is the list of business IDs. Every user in the same cell shares the entry.