Part 6 of 6 · 3 min read
Exercises
Easy
Easy 1: Draw the trip states (20 minutes)
Draw the states of a trip and every allowed arrow. Mark the end states.
Done when: the drawing has five states, four allowed starting arrows and two end states, completed and cancelled.
Hint
Completed and cancelled have no arrows leaving them.
Easy 2: Run the service (20 minutes)
Type the code and reproduce the first four rows of the run table.
Done when: you print D2 ASSIGNED, the fare 13400, the driver D1, and the cancel result.
Hint
The fake drivers sit at fixed points, so the nearest one is easy to see.
Easy 3: Price a trip (20 minutes)
With the made-up rates, price a trip of 3 km and 8 minutes, then the same trip with a 2.0 surge.
Done when: you write 5000 + 3600 + 800 = 9400 and 18800.
Hint
3 km at 1200 is 3600, and 8 minutes at 100 is 800.
Medium
Medium 1: Vehicle types (45 minutes)
Add a type to drivers and requests. The matcher must only offer drivers of the requested type.
Done when: a test with one bike and one car shows each request getting the right type, and a request with no match returning empty.
Hint
Filter inside rank, before sorting.
Medium 2: Decline and retry (45 minutes)
Let a driver decline an assigned trip. The service then offers it to the next driver, and never to the same driver twice.
Done when: a test where the first two drivers decline gives the trip to the third, and where all decline gives empty.
Hint
Keep a set of drivers who declined for this trip.
Medium 3: Stale locations (45 minutes)
Record the time of each location update. The matcher ignores drivers not updated in the last 30 seconds, using an injected clock.
Done when: a test moves the clock and the stale driver is skipped.
Hint
Store a timestamp with each location.
Hard
Hard 1: Grid index (90 minutes)
Replace the full scan with a grid of cells. A request looks at its own cell and then rings of cells outward until it finds a driver.
Done when: results equal the full scan on 1,000 random cases, and a timing table shows the grid faster on 10,000 drivers.
Hint
The cell of a point is its coordinates divided by the cell size, rounded down.
Hard 2: Race at scale (90 minutes)
Run 1,000 riders and 200 drivers on 16 threads, with trips completing at random. Check that no driver ever holds two trips, and that the counts add up.
Done when: a check prints "no double assignment" and requested equals assigned plus failed.
Hint
Record each reserve and release in a log, and replay it to check.
Hard 3: Payment failure (90 minutes)
Add a payment gateway that can fail on complete. Decide the rule and implement it: the trip stays completed and the debt is recorded, or the completion is undone.
Done when: a test with a failing gateway leaves a consistent driver state, and a note explains the rule chosen.
Hint
The compensating action of Level 6, module 5, is a useful pattern here.