KatInterview

Build a Backend for On-Demand Car Rides and Driver Dispatch

System DesignModel answer
MediumModel answerSystem Designsystem-designgeospatial-indexdispatch

Last updated

Design a service that pairs passengers with close-by available drivers, ingests live driver positions, and carries each trip from request to payment without ever double-booking a driver.

You are asked to design the server side of an on-demand taxi platform. A passenger picks a pickup location and a destination, the platform finds a suitable driver nearby, sends that driver an offer, and then follows the trip until it ends.

Treat the discussion as one you are leading. Begin by pinning down requirements, then build the architecture in stages. For each component, explain why it exists, compare alternatives, and point out where the design could be improved. Your interviewer may interject with changes midway, so keep the overall structure coherent while absorbing them.

Scope to assume

  • Passengers can view a fare estimate, request a ride, and follow the assigned driver's approach in near real time.
  • Drivers switch between online and offline, and while online they send location updates at regular intervals.
  • Each request is matched with a nearby available driver, who has a short window to accept or decline. A driver must never be committed to two trips at the same moment.
  • Every trip passes through well-defined states: requested, driver assigned, in progress, completed, or cancelled. A finished trip ends in payment, which may be handed to an external payment provider.
  • Not in scope unless raised: pooled rides, pre-booked rides, driver onboarding, and ratings.
  • No traffic numbers are provided. Pick your own assumptions, state them, and size the system from them.

Questions worth settling early

  • Is the matching goal shortest straight-line distance, shortest estimated arrival time, or a broader score that also considers acceptance likelihood?
  • Is each request offered to a single driver at a time, or to several simultaneously?
  • How old can a driver's reported position be before match quality degrades noticeably?
  • Is surge pricing part of the design?
  • Is the launch a single region, or several regions with data-residency rules?

What to deliver

Cover requirements and scale estimates, the main entities and APIs, a trip state machine, a way to search for nearby moving drivers, a dispatch flow that avoids double assignment and handles declines, timeouts and cancellations, real-time delivery to both apps, and the behavior when components or devices fail.

Hints

Hint 1

Start with numbers: how many online drivers, how often each reports a position, and how many ride requests per second at peak. The location write rate will dominate and should drive your storage choice.

Hint 2

Driver positions change constantly, so a durable disk-based database is a poor home for them. Think about an in-memory store partitioned by a grid or hierarchical cell id (geohash, S2, H3), and query the rider's cell plus its neighbors.

Hint 3

Double assignment is a concurrency problem. Use an atomic claim on the driver (compare-and-set or a short lease with expiry) before sending the offer, and make the trip state transitions idempotent and versioned.

Hint 4

Decide up front what happens when an offer times out, a driver app drops offline mid-offer, or the dispatch service crashes. Offers with expiry timestamps and a durable trip record let another worker resume.