Design a ride sharing service
hard 90 minModel riders, drivers, offers and trips as objects.
Video lesson · Chapter 1 of 6
Make a prediction
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
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
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
criticalTwo requests find the same driver before either assigns.
Wrongdriver = matching.find_best(pickup)if driver: driver.accept_trip(trip)Rightdriver = pool.claim(pickup, type, rule, skip) # marks OFFEREDif driver is None: ... # an ordinary answerCancel forgets the offer
criticalA driver who was only being asked stays OFFERED for ever.
Wrongif trip.driver: pool.release(trip.driver)Rightheld = trip.driver or trip.offered_toif held is not None: pool.release(held)Charging before the release
criticalA slow or failing bank keeps a driver off the road.
Wrongpayments.charge(key, rider, cents)pool.release(trip.driver)Rightpool.release(trip.driver) # under the lock, with the movepayments.charge(key, rider, cents) # later, repeatableDegrees as distance
majorA limit of 5 means 5 degrees: hundreds of kilometers.
Wrongmath.sqrt((lat1 - lat2) ** 2 + (lon1 - lon2) ** 2) < 5Rightpickup.meters_to(driver.place) <= 5000Going online as release
majorA driver who pressed stop is put back on the map.
Wrongdriver.go_online() # in cancelRightpool.release(driver) # OFFLINE if it was the last rideOne surge for the world
majorA rainy corner raises every price, and rides change price while driving.
Wrongservice.set_surge_multiplier(2.0)Rightpercent = 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.