AlgoViz

Design Yelp

medium 50 min

Build 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

Our systemPhoneGatewaySearch serviceRediscells, cardsSearch indexRanking

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

PlacekmStarsReviewsSimple score
Corner0.07240
Great0.354.52500
One review0.551

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

    critical

    Six-letter cells are under 1 km. Nine of them cannot cover 5 km.

    Wrong
    cells = [c] + neighbours(c)   # 6 letters# radius 5 km
    Right
    cells = cover(point, radius)   # size from radiushits = [p for p in places(cells) if km(point, p) <= radius]
  • Cutting before filtering

    critical

    The nearest 500 may miss most pizza places.

    Wrong
    ids = nearest(point, 500)ids = [i for i in ids if "pizza" in words(i)]
    Right
    index.query(within=radius, words="pizza", limit=200)
  • Opening hours in UTC

    major

    They are wrong by an hour for part of the year.

    Wrong
    open_utc = "14:00"
    Right
    hours = [("mon", "10:00", "22:00")]time_zone = "America/New_York"
  • Updating a rounded average

    major

    Small changes are lost and errors add up.

    Wrong
    avg = round((avg * n + new) / (n + 1), 1)
    Right
    stars += newn += 1            # avg = stars / n
  • Writing to two stores yourself

    major

    A crash between the writes leaves them different for ever.

    Wrong
    db.save(review)index.save(review)
    Right
    db.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.