Design Yelp
medium 50 minBuild a service like Yelp: find good places near a point, read about them and write reviews.
See it run
Local search, one hop at a time: finding places, covering a circle, writing reviews, catching fakes, ranking and caching.
Operations
Search near me
Pizza within 5 km: cells and words find candidates, exact checks and ranking choose twenty.
Step 1 of 13
The request
words
pizza
radius
5 km
A point in Times Square, the word pizza and 5 km. Why: the phone knows where it is; an address would be turned into a point first.
Step 1: The request
def search(point, words, radius, filters): cells = cover(point, radius) found, missing = cache.get_cells(cells, words) if missing: found += index.query(missing, words) # words and place together near = [p for p in found if km(point, p.point) <= radius and passes(p, filters)] best = top(near, 200, key=simple_score) try: best = ranking.order(best, timeout_ms=40) except Timeout: pass # keep the simple order return cards(best[:20])- Latency
- < 200 ms p99
- Load
- 3,125/s
- Peak
- 9,400/s
- Ranking limit
- 40 ms
Every filter runs before the cut to 200. A cut that comes first loses answers.
In the real world
The same design yelp doing real jobs. Pick one and step through it.
Three pizza places and one simple score: half nearness, half smoothed rating.
Step 1 of 4
By distance
| Place | km | Stars | Reviews | Simple score |
|---|---|---|---|---|
| Corner | 0.07 | 2 | 40 | |
| Great | 0.35 | 4.5 | 2500 | |
| One review | 0.5 | 5 | 1 | |
Nearest: Corner
Corner is 70 m away but has two stars.
Step 1: By distance
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Nine small cells for a large radius
criticalSix-letter cells are under 1 km. Nine of them cannot cover 5 km.
Wrongcells = [c] + neighbours(c) # 6 letters# radius 5 kmRightcells = cover(point, radius) # size from radiushits = [p for p in places(cells) if km(point, p) <= radius]Cutting before filtering
criticalThe nearest 500 may miss most pizza places.
Wrongids = nearest(point, 500)ids = [i for i in ids if "pizza" in words(i)]Rightindex.query(within=radius, words="pizza", limit=200)Opening hours in UTC
majorThey are wrong by an hour for part of the year.
Wrongopen_utc = "14:00"Righthours = [("mon", "10:00", "22:00")]time_zone = "America/New_York"Updating a rounded average
majorSmall changes are lost and errors add up.
Wrongavg = round((avg * n + new) / (n + 1), 1)Rightstars += newn += 1 # avg = stars / nWriting to two stores yourself
majorA crash between the writes leaves them different for ever.
Wrongdb.save(review)index.save(review)Rightdb.save(review)# consumers follow the change stream
Interview tips
Draw the border
- One cell misses the cafe over the road.
- Nine cells have a limit.
- Measure at the end.
Bring numbers
- 9,400 searches a second at peak.
- 2.3 reviews a second.
- 20 GB of points.
Protect trust
- Smooth the rating.
- Hold, do not count.
- Give owners an appeal.
Know where to stop
- A store finder needs one database.
- Words bring the index.
- Lunch brings the cache.