Cab Booking
medium · nearest-neighbour matching, ride lifecycle, fare
Brief-reported interview attribution (unverified): Uber, Lyft, Ola
How to approach an LLD roundDesign a ride-hailing system. Drivers and riders have (x, y)
coordinates. request_ride matches the nearest available driver to the rider
(Euclidean distance; ties break to the lowest driver id). A ride moves through
"requested" → "ongoing" → "completed" (or "cancelled" from "requested"
only). Fare is $5 + $2 × distance(pickup, destination), rounded to 2 decimals.
On completion the driver becomes available at the destination.
class RideHailing:
def __init__(self):
def add_driver(self, driver_id, x, y):
def add_rider(self, rider_id, x, y):
def request_ride(self, rider_id, dest_x, dest_y):
# -> ride id (int, starting at 1), or None if the rider is unknown or
# no driver is available
def start_ride(self, ride_id):
# -> True if the ride was "requested" and is now "ongoing", False otherwise
def complete_ride(self, ride_id):
# -> fare (float) if the ride was "ongoing", None otherwise
def cancel_ride(self, ride_id):
# -> True if the ride was "requested" and is now cancelled, False otherwise
def ride_status(self, ride_id):
# -> "requested" | "ongoing" | "completed" | "cancelled" | "unknown"
def driver_location(self, driver_id):
# -> (x, y), or None for an unknown driverRules. A driver is busy from the moment a ride is requested until it is completed or cancelled; a busy driver is never matched. Cancelling frees the driver at their current location (unchanged). Fares use the pickup location recorded at request time.
Money units: fares are dollars, rounded to two decimal places; a distance of 4 returns 13.0.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Public demo
# Public demo: run after implementing the class API above.
r = RideHailing()
r.add_driver('D1', 0, 0)
r.add_driver('D2', 10, 0)
r.add_rider('R1', 3, 0)
print(r.request_ride('R1', 3, 4))
print(r.ride_status(1))Starter code
class RideHailing:
def __init__(self):
"""Initialize the state described in the problem."""
pass
def add_driver(self, driver_id, x, y):
"""Implement the return value and rules in the API contract."""
pass
def add_rider(self, rider_id, x, y):
"""Implement the return value and rules in the API contract."""
pass
def request_ride(self, rider_id, dest_x, dest_y):
"""-> ride id (int, starting at 1), or None if the rider is unknown or no driver is available"""
pass
def start_ride(self, ride_id):
"""-> True if the ride was "requested" and is now "ongoing", False otherwise"""
pass
def complete_ride(self, ride_id):
"""-> fare (float) if the ride was "ongoing", None otherwise"""
pass
def cancel_ride(self, ride_id):
"""-> True if the ride was "requested" and is now cancelled, False otherwise"""
pass
def ride_status(self, ride_id):
'''-> "requested" | "ongoing" | "completed" | "cancelled" | "unknown"'''
pass
def driver_location(self, driver_id):
"""-> (x, y), or None for an unknown driver"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler