Full Mock Interview: Designing a Ride-Sharing Matching Engine
You'll learn to
- -Follow a 45-minute LLD answer on an unfamiliar prompt, minute by minute
- -See where the earlier patterns (State, Strategy, an atomic find-and-claim) turn up again without being asked for
- -Practice closing with trade-offs and what you deliberately left out
Every earlier case study was picked because it is a well-known problem you may already recognize. This chapter picks a less rehearsed prompt on purpose: match riders to nearby drivers, the core of a ride-hailing app. It works through it live, the way you would actually meet it, with no memorized "expected" answer. The clock labels below show how a 45-minute round is spent.
Minute 0 to 5: Clarify Requirements
- -What triggers a match? A rider requests a ride, and the system finds a nearby available driver.
- -What does "nearby" mean? Do we need real geographic search, or can we simplify to straight-line distance for this exercise?
- -Can a driver decline? If so, should the system try the next-nearest driver automatically?
- -Is pricing or surge in scope, or only matching? Assume matching only.
- -One request at a time, or many at once? Assume many, concurrently.
Each question either changes what gets built or surfaces a hidden concurrency or scale concern. These five minutes are the highest-leverage of the whole round.
Minute 5 to 15: Identify Classes and Relationships
Underline the nouns: location, driver, ride request, match. Each becomes a class. Make Driver.status an object, not a plain string. That is a deliberate early choice. What a driver can do differs between Available, EnRoute and Offline, which is exactly the shape the State pattern was built for.
class Location:
def __init__(self, lat: float, lng: float):
self.lat = lat
self.lng = lng
class Driver:
def __init__(self, driver_id: str, location: Location):
self.driver_id = driver_id
self.location = location
self.status: "DriverStatus" = AvailableStatus()
class RideRequest:
def __init__(self, rider_id: str, pickup: Location):
self.rider_id = rider_id
self.pickup = pickup
class Match:
def __init__(self, ride_request: RideRequest, driver: Driver):
self.ride_request = ride_request
self.driver = driverMinute 15 to 30: The Matching Algorithm and Concurrency
"Find the nearest available driver" is itself a swappable algorithm. Today it means nearest by distance. A real system might weigh driver rating, arrival time or demand balancing. That is a clean Strategy opportunity. Then comes the part to get right: finding a driver and claiming them must be one atomic step, or two riders could be given the same driver.
class MatchingStrategy(ABC):
@abstractmethod
def find_driver(self, request: RideRequest, drivers: list[Driver]) -> Driver | None: ...
class NearestAvailableStrategy(MatchingStrategy):
def find_driver(self, request: RideRequest, drivers: list[Driver]) -> Driver | None:
available = [d for d in drivers if isinstance(d.status, AvailableStatus)]
if not available:
return None
return min(available, key=lambda d: distance(d.location, request.pickup))
class MatchingEngine:
def __init__(self, strategy: MatchingStrategy):
self._strategy = strategy
self._lock = threading.Lock()
def match(self, request: RideRequest, drivers: list[Driver]) -> Match | None:
with self._lock: # atomic: find-and-claim, not find-then-claim
driver = self._strategy.find_driver(request, drivers)
if driver is None:
return None
driver.status = EnRouteStatus()
return Match(request, driver)This is the same check-then-act discipline as the parking lot, applied here without being prompted. That is the transfer this course aims for: recognizing the shared, limited resource on a brand new problem.
Minute 30 to 40: Handling Decline and Retry
When the interviewer asks "what if the matched driver declines?", the answer builds on what exists. The engine already skips drivers who are not available, so a decline just sets the driver back to available and calls match again with the same request, this time excluding the driver who said no. No new architecture, only a small addition.
The demo runs the whole story. Two riders are matched, one driver declines and the retry finds the next driver. Then four riders ask at once for three drivers.
Minute 40 to 45: Trade-offs and What Was Left Out
- -A linear scan over all drivers stands in for real geographic indexing (a quadtree or geohash). That is fine for a small fleet and the first thing to fix at real scale.
- -Nothing here is distributed. It is a single-process engine, which fits this course's LLD scope. An HLD round would ask how it scales across regions and servers.
- -Pricing, arrival-time estimates and driver ratings were scoped out at minute 0 and are named again as the natural next layers.
Closing with a deliberate "here is what I would improve with more time" list, instead of letting the clock simply run out, shows awareness of the design's limits and not only its features.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!