AlgoViz

Design a home rental marketplace like Airbnb

hard 60 min

Guests search homes by place and dates, hold some nights, pay, and stay.

See it run

Search, hold, pay, sync calendars and suggest prices, one hop at a time.

Operations

Search a city

Each step leaves fewer homes for the next, so the costly steps see few.

Step 1 of 11

The guest asks

Our systemGuestGatewaySearch serviceRedisarea cacheSearch indexNight bitmapsPricingRanker

nights

15 to 19

guests

2

Paris, March 15 to 20, two guests. Why: place, dates and guests decide what can be shown at all.

Step 1: The guest asks

def search(q):    where = geocode(q.text)                    # place to lat, lng    ids = cache.get(area(where, q.guests))    if ids is None:        ids = index.near(where, km=25, guests=q.guests, limit=1000)        cache.set(area(where, q.guests), ids, ttl=300)    free = [i for i in ids if bitmap[i] & q.mask == q.mask]    priced = [price(i, q) for i in free]    ranked = rank(priced, q.user)    return mix(ranked)[:20]
Time
about 122 ms
Load
10,500/s peak
Ask the calendar table
10.5M checks/s

Cache candidates for an area, not whole answers. Apply the bitmap last, so a cached list is still filtered by fresh nights.

In the real world

The same home rental marketplace doing real jobs. Pick one and step through it.

The provider took the money. Our server died before it heard.

Step 1 of 5

PAYING is stored

Our systemGuestCard providerBooking servicePostgreSQLnightsPayment serviceEvent streamNotices

state

PAYING

Before the call, the booking says PAYING with its key.

Step 1: PAYING is stored

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A lock named after the date range

    critical

    Overlapping ranges have different names, so both locks are granted.

    Wrong
    redis.set("lock:456:0315:0320", me, nx=True)redis.set("lock:456:0317:0322", you, nx=True)  # also ok
    Right
    # one row for each night, all in one transactionn = take_if_free(listing, nights)assert n == len(nights)
  • Releasing nights before cancelling the payment

    critical

    The payment can still succeed, and the nights belong to somebody else.

    Wrong
    release(nights)provider.cancel(payment)
    Right
    if provider.cancel(payment).cancelled:    release(nights)
  • Trusting the price in the request

    critical

    Anyone can send a smaller number.

    Wrong
    charge(request["total"])
    Right
    charge(quote_of(hold_id).total)
  • Nights stored as UTC times

    major

    March 15 in Tokyo becomes two dates elsewhere, and a clock change makes a night 23 hours.

    Wrong
    check_in = datetime(2027, 3, 15, tzinfo=UTC)
    Right
    check_in = date(2027, 3, 15)   # the home's local date
  • Sending mail inside the booking transaction

    major

    A slow mail server holds locks, and a rollback cannot unsend.

    Wrong
    with db.transaction():    confirm(b); send_mail(b)
    Right
    with db.transaction():    confirm(b); outbox.add("confirmed", b.id)

Interview tips

  • Open with the split

    • 10,500 searches, 17 bookings a second.
    • Looking may be stale, booking may not.
  • Define the unit

    • One listing, one local date.
    • Half-open stays.
    • All or nothing.
  • Follow the money

    • State before call.
    • Same key on retry.
    • Two ledger sides, daily check.
  • Expect trouble

    • Reply lost: stay PAYING.
    • Other site: report conflicts.
    • Stream down: refusals rise, nothing breaks.