Design a Ride-Sharing Matching Engine

Medium
Real World SystemsGeolocationMatching AlgorithmRoute OverlapPriority QueueConcurrency

Problem Statement

Design the core matching engine for a ride-sharing platform like BlaBlaCar or Uber Pool — where drivers offering rides along a route are matched to riders whose pickup/drop-off points fall within the route.

Requirements

Functional Requirements

  • Driver offers a ride: source, destination, departure time, available seats, vehicle type, price per km
  • Rider requests a ride: pickup location, drop location, desired departure window
  • Matching algorithm: find drivers whose route covers the rider's pickup and drop-off
  • Rank matches: by price, ETA, or rating
  • Rider books one of the matched rides (seat deducted)
  • Driver sees all booked riders for their trip
  • Trip completion and rating (both sides)
  • Cancel ride: driver cancels (all riders notified), or rider cancels (seat freed)

Non-Functional Requirements

  • Thread-safe seat booking
  • Efficient route matching (proximity checks)

Key Entities

  • Ride: id, driver, source, destination, departureTime, totalSeats, availableSeats, price, waypoints
  • RideRequest: id, rider, pickupLocation, dropLocation, desiredTime
  • Booking: rideId, riderId, pickupStop, dropStop, fare
  • MatchingService: findMatches(RideRequest) → List<Ride>
  • Driver/Rider: profile entities

Constraints

  • Route overlap check: rider's pickup and drop must fall within driver's origin-destination corridor (configurable radius)
  • Available seats must be > 0 for matching
  • Departure time window: rider's desired time within ±1 hour of driver's departure
  • Thread-safe seat decrement

!Constraints

- Rider pickup and drop must fall within driver route corridor (±5 km radius from route) - Departure time match within ±1 hour window - Seat booking is atomic — concurrent bookings cannot oversell - Driver minimum 1 available seat required to appear in matches - Cancellation penalty applies within 1 hour of departure
Output

Run or submit your code to see output here.