Design a Ride-Sharing Service
🚕 What is a ride-sharing service? It is the app that connects a person who needs a ride with a driver nearby. The rider says where they are and where they want to go, the app picks a driver, the driver picks the rider up and drops them off, and the app charges a fare. Uber and Lyft are the well-known examples.
You will hear this prompt as "Design Uber", "Design Lyft" or "Design a ride-hailing app". In a low-level design round they all mean the same thing: the classes inside one service that match riders with drivers, move each trip through its states, and work out what the rider pays. The distributed version, with location streams, geo-sharding and millions of drivers, is a system design question; see /system-design/ride-sharing for that side. Here we build the core that such a system would run.
Requirements
"Design a ride-sharing service like Uber. Riders request rides, drivers accept them, and the rider pays at the end."
The prompt hides most of the rules. The questions below are the ones that change the classes or the code, and each answer becomes a rule we will build against.
Clarifying Questions
You: Are there different kinds of rides?
Interviewer: Two: economy and premium. A driver offers exactly one of them, based on their car. A premium request needs a premium driver, and the other way round.
You: How do we choose the driver for a request?
Interviewer: The closest free driver of the right type. Use city-block distance on a grid, and if two drivers are equally close, take the smaller driver id so the result is predictable.
You: Does the driver get to accept or decline the request?
Interviewer: Not for now. Assign the driver at once. If no driver is free, tell the rider there are no cars available. No waiting queue.
You: What states does a trip go through, and when can it be cancelled?
Interviewer: Assigned, in progress, completed, or cancelled. The rider can cancel only before pickup. Cancelling within 5 minutes of the request is free; after that the rider pays the base fare of the ride type.
You: How is the fare computed?
Interviewer: A base fare, plus a rate per block of the route from pickup to drop-off, plus a rate per minute from pickup to drop-off. Economy is 3 base, 2 per block, 1 per minute. Premium is 5, 3 and 2. Expect surge pricing later.
You: Can a rider have two rides at the same time? Can a driver go offline?
Interviewer: One open trip per rider. Drivers can go offline and come back, but not in the middle of a trip. Drivers also send location updates as they move.
You: How do we know the time?
Interviewer: Pass a timestamp in minutes with each call that needs one.
The third answer saves us a lot of work. Without driver acceptance, matching is one synchronous step: the request either gets a driver or it fails. With acceptance, a trip would sit in an "offered" state, waiting on a human, with time-outs and retries to the next driver. That is a fine follow-up, and we come back to it in Extensibility, but it is not where the first 30 minutes should go.
💡 Interview tip: When the prompt names a famous product, scope it down out loud. "I'll design the matching, the trip life cycle and the fare. Payments, maps and the driver app are out." Interviewers want to see you pick the part that has real rules, not try to build all of Uber.
Final Requirements
- Drivers register with an id, a ride type (
ECONOMYorPREMIUM) and a location. They can go offline and online, but not during a trip, and they can update their location at any time. - A rider requests a ride of one type from a pickup point to a drop-off point. A rider with an open trip (assigned or in progress) cannot request another.
- The request is matched at once to the closest driver who offers that ride type, is online and is not on a trip; ties go to the smallest driver id. If there is none, the request fails.
- A trip moves
ASSIGNED → IN_PROGRESS → COMPLETED, orASSIGNED → CANCELLED. Any other move is rejected. - On pickup the driver is at the pickup point. On drop-off the driver is at the drop-off point and is free again.
- The fare is
base + per_block × route blocks + per_minute × minutes in the car, with rates per ride type. - A cancellation is free within 5 minutes of the request and costs the base fare after that.
- Pricing will change (surge, promotions), and so may the matching rule, so both must be swappable.
Out of scope
- Payments, ratings, receipts and tips.
- Maps, routing and real ETAs; we use grid distance.
- Driver acceptance and a waiting queue (Extensibility).
- Shared or pooled rides.