AlgoViz

Design a ride sharing service

hard 90 min

Model riders, drivers, offers and trips as objects.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:42

See it run

Calls travel between objects, the two lives change together, and the matching lines light up.

Operations

Request and offer

A quote becomes a trip, and the nearest free driver is asked.

Step 1 of 6

A price is agreed

ownsNoor's appcallerserviceRideServiceridingnonelockfreepoolDriverPoolAliceAVAILABLECharlieAVAILABLE

Noor has quote 1: 1250 cents, surge 1.0. Two economy drivers are free.

Step 1: A price is agreed

def request(self, rider_id, quote_id, key):    with self._lock:        if (rider_id, key) in self._by_key:            return self._by_key[rider_id, key]        quote = self._quotes.get(quote_id)        if quote is None or quote.rider_id != rider_id or self._now() > quote.valid_until:            raise RideError("no valid quote")        if rider_id in self._riding:            raise RideError("already riding")        trip = Trip(next(self._ids), quote)        self._trips[trip.id] = trip        self._by_key[rider_id, key] = trip        self._riding[rider_id] = trip.id        self._offer(trip)    self._tell(trip)    return trip
Cost
O(n) drivers
Guarantee
One driver, one offer
With cells
a few dozen looks

The key is checked before everything else.

In the real world

The same ride sharing doing real jobs. Pick one and step through it.

The factor of a street changes three times. Noor's does not.

Step 1 of 5

Asked

Noor's appcallersurgeSurgecove200serviceRideServiceridingnonelockfreepercent(cove)200

Ten riders and three free drivers. The area is at 200.

Step 1: Asked

Common mistakes

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

  • Find, then assign

    critical

    Two requests find the same driver before either assigns.

    Wrong
    driver = matching.find_best(pickup)if driver:    driver.accept_trip(trip)
    Right
    driver = pool.claim(pickup, type, rule, skip)   # marks OFFEREDif driver is None:    ...                                           # an ordinary answer
  • Cancel forgets the offer

    critical

    A driver who was only being asked stays OFFERED for ever.

    Wrong
    if trip.driver:    pool.release(trip.driver)
    Right
    held = trip.driver or trip.offered_toif held is not None:    pool.release(held)
  • Charging before the release

    critical

    A slow or failing bank keeps a driver off the road.

    Wrong
    payments.charge(key, rider, cents)pool.release(trip.driver)
    Right
    pool.release(trip.driver)      # under the lock, with the movepayments.charge(key, rider, cents)   # later, repeatable
  • Degrees as distance

    major

    A limit of 5 means 5 degrees: hundreds of kilometers.

    Wrong
    math.sqrt((lat1 - lat2) ** 2 + (lon1 - lon2) ** 2) < 5
    Right
    pickup.meters_to(driver.place) <= 5000
  • Going online as release

    major

    A driver who pressed stop is put back on the map.

    Wrong
    driver.go_online()     # in cancel
    Right
    pool.release(driver)   # OFFLINE if it was the last ride
  • One surge for the world

    major

    A rainy corner raises every price, and rides change price while driving.

    Wrong
    service.set_surge_multiplier(2.0)
    Right
    percent = surge(pickup)        # read once, kept in the quote

Interview tips

  • Open with

    • Two lives: trip and driver.
    • One owner of drivers.
    • Asking is not assigning.
  • Reject aloud

    • A rider is an id.
    • A vehicle type is an enum.
    • No managers that forward.
  • Prove it

    • Two riders, one driver.
    • A late accept.
    • Cancel during an offer.
  • Never

    • A bank under a lock.
    • A price from the client.
    • Any state to cancelled.