Ride Hailing Service
On-Demand Transportation Platform
Design the object model of a ride hailing service: riders request trips, a grid index finds the nearest free drivers, offers go out one driver at a time, fares come from a swappable policy, and a trip moves through a fixed set of states.
Overview
A rider asks for a car from one place to another. The service finds free drivers near the pickup, offers the trip to the nearest one, moves on if that driver declines, and once a driver accepts it tracks the trip until drop-off and charges a fare. Drivers move all the time, so the structure that answers 'who is near this point' must be cheap to update as well as to query.
This is a design exercise; it does not describe how any real company builds its dispatch. Interviewers pick it because it combines a spatial lookup (a grid index), a two-sided handshake (offer, accept or decline), a pluggable price (fare policy with surge as a wrapper), and a race (two riders must not be offered the same driver). A strong answer draws the trip states first, keeps the index as a dictionary of cells, offers to one driver at a time, and puts the whole offer step under one lock.
Money is whole cents, and the fare is computed by a policy object so that surge, promotions and vehicle classes are new classes rather than new branches.
Scoping the problem
The design depends on six questions. For each, here is why it matters and the usual answer.
- Offer to one driver at a time, or broadcast to several? Broadcast needs a first-accept-wins rule; one at a time needs a timeout. Usually one at a time, nearest first, with decline and timeout.
- One vehicle class or several? A class is a filter on candidates and a different fare policy. Start with one, and show where the filter and the policy go.
- How is the fare estimated before the trip? Straight-line distance at an assumed speed is enough for the model. Estimate up front, and charge the measured trip at the end.
- Is surge in scope? Surge is a multiplier on the base policy, not a new lifecycle. Yes, as a wrapper around the standard fare.
- How often do drivers report position? That sets the update rate the index must absorb. Every few seconds, and the index update is O(1).
- Payments, ratings, maps? Each is its own service. They are out of scope; name the interface.
Functional requirements
- Request a trip from a pickup to a drop-off; the trip gets an estimate in cents up front
- Find the free drivers within a radius of the pickup, nearest first, from a grid index that drivers move through
- Offer the trip to one driver at a time; a decline moves the offer to the next candidate; no candidate ends the trip as NoDriver
- Move a trip through Requested, Offered, Matched, Arrived, InProgress and Completed, refusing any other move
- Charge the completed trip from the measured distance and time through the fare policy, with surge as a wrapper
- Cancel a trip and free its driver
Non-functional requirements
- A driver holds at most one offer or trip at a time: the offer step runs under one lock
- Index update on a position report is O(1): remove from the old cell, add to the new one
- The fare policy is swappable at runtime without changing the dispatcher or the trips
- Money is integer cents; estimates and fares are both kept on the trip
- Every status change is recorded in the trip history
Interview questions
- Why offer to one driver at a time instead of broadcasting to everyone nearby?
- How does surge pricing fit the design?
- Drivers report a new position every few seconds. How does the index keep up?
- Two riders request at the same moment and there is one free driver. Walk through what happens.
- What do you store, and how do you handle a driver app that sends accept twice?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio