Design a ride sharing service 00:00:00 Make a prediction You will distinguish finding a nearby driver from assigning that driver and model the losing request explicitly. Two passengers receive the same nearest-driver result. Does the distance search guarantee that only one of them can start a trip with that driver? No. A search returns candidates using potentially stale information. Recheck and claim availability under a single owner when the offer is accepted. 00:00:29 One morning with three donkeys A village has three donkey drivers and a board showing who is nearby. Two customers can point to the same name. Only taking the driver's single availability tag prevents both from promising the same ride. The board is discovery; the tag is assignment. Pricing, proximity and ownership are different decisions with different consistency needs. A ride sharing service in a low-level design round is the program between a person who wants to go somewhere and a person with a car. It names a price, finds a free driver, asks them, follows the ride and collects the money. It mixes the classic themes of an object design round in one story: a state machine, two strategies, observers and a race that is easy to explain and easy to get wrong. 00:01:20 Assign a driver exactly once Two nearby passengers request rides and one driver is available. Proximity finds a candidate; it does not reserve that driver. The assignment step must check availability and claim the driver once. Accepting an offer creates a trip with a stable driver and rider identity. A second offer that arrives late must fail or expire. The first passenger's confirmation cannot be replaced because a later distance calculation finds the same driver. Two dispatchers independently see the same available driver. The first conditional assignment succeeds. The second must consult the next candidate even if its list is only milliseconds old. Separating candidate discovery from claiming the driver makes a slightly stale spatial index tolerable. 00:02:08 Cancel at a meaningful boundary A passenger cancels before pickup. The trip moves to cancelled and the assigned driver becomes available again according to policy. If the driver already began the trip, cancellation may obey different rules. Model those transitions explicitly and reject impossible ones. Cancel once before pickup and once after the trip begins. The earlier cancellation can free the driver under the stated policy; the later one may need a fare or a different terminal state. A single global canceled flag cannot express those different downstream obligations. Writing a second offer into a trip record is not a safe conflict resolution if the first passenger was already confirmed. Test both responses, not only the last database value. A simple nearest-available policy suffices for a small system; keep it separate from the atomic assignment mechanism. H3. An open source grid of hexagons over the globe, developed to address the data science needs of Uber. Cells have a resolution, and neighbours within k steps can be listed: the rings of the matching page. S2 Geometry. A library that works on a sphere and not on a flat map. It maps the globe to cells with 64 bit ids, so cells can be keys of an ordinary index. 00:03:29 Try an interview follow-up How do you find near drivers among 50,000? I would use a cell index to find nearby candidates and expand the search until a distance lower bound rules out a closer unexplored driver. The index can be slightly stale. The final claim must still change one driver from available to offered atomically; on a conflict, try the next eligible candidate. Two drivers accept the same offer before either sees the other's response. Make the trip's pending-to-assigned transition conditional on its current state. Send the winning driver's identity back and tell the second driver the offer expired. A retry from the winner should reveal the existing assignment. 00:04:15 What to take with you Two lives: a trip with six states and a driver with four. One pool changes drivers and claims in one step. A quote freezes price facts, an offer may be refused, and matching and fares are rules from outside. Complete writes the fare, frees the driver and then charges with the trip as key. Cancel frees a driver who was only offered too. Finding may be old, taking may not.