~learn.
0:00 / 1:08
LLD · Ride Sharing

Two riders, one nearest driver, the same second

transcript

Riya and Kabir tap book. Same corner, same moment. Both searches say the nearest driver is Ravi.

Riya's search returns Ravi, available. Kabir's search returns Ravi too. Search, then assign, and both assign him. One driver, two pickups.

Search again just before assigning? Same problem. That's only another snapshot. Stale the moment it returns.

So the check can't live in the search. It lives on the driver. Is Ravi still available? Then he's on this trip. Atomically, under Ravi's own lock. Only riders who want Ravi ever wait. Exactly one call sees available. The other gets false, and tries the next driver.

Outside a stadium, 20 riders tap at once. Three drivers nearby. Three matched. Seventeen told no, at once. Not a spinner. Drivers double-booked: zero. On every run. Timing picks who wins, never whether.

The search is a snapshot. The reservation is a compare-and-set. So two riders never get the same car.

Full breakdown in the lecture.