Part 5 of 6 · 3 min read
Edge cases and changes
Timeouts, scale and fairness.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Driver declines or does not answer | Release the driver and offer the trip to the next one in the ranked list. Add a timeout |
| Rider cancels after the trip started | Not allowed in this model. A real service may charge a fee |
| Driver goes offline mid-trip | Only an available driver can go offline, so a driver on a trip cannot |
| Two riders ask for the same nearest driver | One reserve succeeds. The other moves to the next driver |
| Request at the same spot as the destination | Fare is only the base and the time charge |
| Driver location is stale | Update the location regularly, and exclude drivers whose last update is too old |
| Completing a trip twice | The second call fails, since the move is illegal |
A weakness to say aloud: the nearest-driver rule scans every driver. That is fine for a few hundred drivers and too slow for a city. A real system keeps drivers in a spatial index, such as a grid of cells, and looks only at nearby cells. The Matcher interface hides this, so the change touches one class.
Tests to write first: each row of the run table; each illegal trip move; the race test; a tie between two drivers at the same distance, broken by id so the result repeats; and a driver who is released and then matched again.
Change requests and the part that changes:
- "Different vehicle types, such as bike, car and van." A type on the driver and the request, and a filter in the matcher. The fare rule may differ by type.
- "Pick the driver with the best rating among the nearest five." A new
Matcher. - "Surge in busy areas." Choose the
Surgewrapper by area and demand. - "Ride sharing." A trip with several riders. This changes the trip model and the matcher a good deal.
- "Notify rider and driver on every change." Observers on the trip (Level 4, module 2).
- "Scheduled rides." A request with a future time and a job that matches shortly before it.
- "Payment." A step in
complete, with the compensating action of Level 6, module 5, if payment fails.
Common mistake
Finding the nearest free driver, and then marking the driver busy in a second step. Two riders find the same driver, and both assign. Reserve the driver in one atomic step, and move to the next candidate if it fails.
Interview angle
Answer in your head first, then open it.
How do you avoid giving two riders the same driver?
The driver has an atomic try-reserve. The service tries drivers in rank order until one reserve succeeds.
How do you model a trip?
A state machine with a table of legal moves, so illegal calls are refused.
How do you change the matching rule?
A matcher interface. The nearest rule is one class.
How does this scale to a city?
A spatial index for nearby drivers, a location service updating positions, and sharding by region.
Check yourself: 1. Which change request needs a spatial index and why? 2. What should happen if a driver does not answer? 3. Which change request would alter the trip model itself?
Words you will meet
| Word | Meaning |
|---|---|
| Pickup and drop-off | The start and end points of a trip |
| Matching | Choosing which driver serves which rider |
| Surge | A price multiplier used when demand is high |
| Reserve | To claim a driver so nobody else can |
| Spatial index | A structure that finds nearby points quickly |