AlgoViz

Design a food delivery service like DoorDash

hard 60 min

A customer orders, a kitchen cooks, a courier carries, and all three watch the same order while the food cools.

See it run

Place an order, match a courier, estimate the minutes, follow the dot, find a kitchen and set a busy-time fee.

Operations

Place an order

The order is stored before money moves, and every later step can be repeated.

Step 1 of 10

Place order

Our systemCustomerCard providerGatewayOrder serviceRestaurant svcPostgreSQLordersPayment serviceEvent stream

key

k1

total

38.78

Restaurant, items with options, address, payment token, quote and a key made by the app. Why: the key joins retries to the first try.

Step 1: Place order

def place(customer, key, basket, quote_id):    if (old := orders.by_key(customer, key)):        return old                              # a retry    price = restaurants.check(basket, quote_id) # open? in stock?    order = orders.insert(customer, key, price, "CREATED")    held = payments.hold(order.total, key=order.id)    if not held.ok:        return orders.move(order, "CREATED", "CANCELLED")    orders.move_with_outbox(order, "CREATED", "PAID")    return order
Time
p99 < 1 s
Load
174/s at dinner
Charge, then store
money without an order

If the hold gets no answer, leave CREATED. A job asks the provider with the same key, then moves forward or cancels.

In the real world

The same food delivery doing real jobs. Pick one and step through it.

The network drops after Place order and the customer taps again.

Step 1 of 3

First tap

Our systemCustomerCard providerGatewayOrder serviceRestaurant svcPostgreSQLordersPayment serviceEvent stream

key

k1

The order is stored, the money held. The answer is lost on the way.

Step 1: First tap

Common mistakes

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

  • Charging before the order is stored

    critical

    A crash in between leaves held money and no order to find it by.

    Wrong
    payments.hold(total)orders.insert(order)
    Right
    orders.insert(order, "CREATED")payments.hold(total, key=order.id)
  • Accepting by read, then write

    critical

    Two couriers read OPEN and both write.

    Wrong
    if row.state == "OFFERED":    row.courier = me; save(row)
    Right
    UPDATE assignments SET state = 'ACCEPTED' WHERE order_id = :o AND courier_id = :me   AND state = 'OFFERED' AND offer_ends > now()
  • Adding cooking and courier time

    major

    They happen together. The estimate becomes twenty minutes too long.

    Wrong
    eta = cook + arrive + drive
    Right
    eta = max(cook, arrive) + pickup + drive + door
  • A row for every ping in the order database

    major

    52,000 writes a second for data that is old in five seconds.

    Wrong
    db.insert("pings", p)
    Right
    newest.set(courier, p); stream.send(p)
  • Believing an old position

    major

    A courier who went offline still appears in the near search.

    Wrong
    near(kitchen)
    Right
    [c for c in near(kitchen) if now - last_ping(c) < 30]

Interview tips

  • Start with three sides

    • What scale, which features, which limits?
    • Customer, kitchen, courier.
    • One order, two tracks.
  • Size with Little's law

    • 174 × 2,400 s = 418,000 orders.
    • 261,000 couriers, 52,000 pings.
  • Trade-offs to say

    • Wait 10 s, match better.
    • Exact orders, loose positions.
    • Range instead of a minute.
  • Show that it can fail

    • Every state has a timeout.
    • Undo steps for each party.
    • Measure: logs, traces, oldest due order.