Design a ride-sharing service class with nearest-vehicle matching and trip states

Quick Overview

Design the classes for a ride-sharing service that registers riders and autonomous vehicles, matches each ride request to the nearest available vehicle, and moves trips through requested, in-progress and completed states. It tests object modeling, state machine design and careful handling of ties and invalid calls.

Design a ride-sharing service class with nearest-vehicle matching and trip states

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design the internal class structure for a ride-sharing service that manages riders, autonomous vehicles, and trips. The service matches a rider to the nearest available vehicle using Euclidean distance and supports real-time trip status updates. When a ride is requested, the system finds the closest available vehicle to the rider's current location. If several vehicles are equally close, the one with the smallest vehicle ID is selected. A vehicle can serve only one trip at a time, and a rider can have only one active trip at a time. Trips move through three statuses: `REQUESTED` (matched but not started), `IN_PROGRESS` (vehicle en route or carrying the rider), and `COMPLETED` (rider delivered to the destination). When a trip starts, the vehicle moves to the pickup location. When a trip completes, the vehicle moves to the destination and becomes available again. Implement the following methods: - `add_rider(rider_id: int, x: int, y: int) -> None`: register a new rider at location (x, y). - `add_vehicle(vehicle_id: int, x: int, y: int) -> None`: register a new autonomous vehicle at location (x, y). - `request_ride(rider_id: int, dest_x: int, dest_y: int) -> int`: match the rider to the nearest available vehicle and create a trip. Return the trip ID on success, or -1 if no vehicle is available, the rider already has an active trip, or the rider does not exist. - `start_trip(trip_id: int) -> str`: move the trip from `REQUESTED` to `IN_PROGRESS`. The vehicle moves to the pickup location. Return the new status string, or `"INVALID"` if the trip does not exist or is not in `REQUESTED` status. - `complete_trip(trip_id: int) -> str`: move the trip from `IN_PROGRESS` to `COMPLETED`. The vehicle moves to the destination and becomes available again. Return the new status string, or `"INVALID"` if the trip does not exist or is not in `IN_PROGRESS` status. - `get_trip_status(trip_id: int) -> str`: return the trip's current status: `REQUESTED`, `IN_PROGRESS`, `COMPLETED`, or `NOT_FOUND`. - `get_vehicle_location(vehicle_id: int) -> List[int]`: return the vehicle's current `[x, y]`, or `[-1, -1]` if the vehicle does not exist. Example 1 shows a distance tie and the full trip lifecycle: ```text add_rider(1, 0, 0) add_vehicle(10, 3, 4) add_vehicle(7, -4, 3) add_vehicle(5, 6, 8) request_ride(1, 10, 10) -> 1 get_trip_status(1) -> "REQUESTED" request_ride(1, 2, 2) -> -1 get_vehicle_location(7) -> [-4, 3] start_trip(1) -> "IN_PROGRESS" get_vehicle_location(7) -> [0, 0] start_trip(1) -> "INVALID" complete_trip(1) -> "COMPLETED" get_vehicle_location(7) -> [10, 10] get_trip_status(2) -> "NOT_FOUND" ``` Vehicles 10 and 7 are both at distance 5 from the rider and vehicle 5 is at distance 10, so the smaller ID, 7, gets trip 1. The second request fails because rider 1 already has an active trip. Vehicle 7 stays where it is until the trip starts, then moves to the pickup point (0, 0), and ends at the destination (10, 10). Example 2 shows a busy fleet and invalid calls: ```text add_rider(1, 0, 0) add_rider(2, 5, 5) add_vehicle(3, 1, 1) request_ride(1, 9, 9) -> 1 request_ride(2, 0, 0) -> -1 request_ride(4, 0, 0) -> -1 complete_trip(1) -> "INVALID" start_trip(1) -> "IN_PROGRESS" complete_trip(1) -> "COMPLETED" request_ride(2, 0, 0) -> 2 get_vehicle_location(8) -> [-1, -1] ``` Rider 2's first request fails because the only vehicle is busy, and rider 4 was never registered. Trip 1 cannot complete before it starts. Once it completes, vehicle 3 is free again at (9, 9) and serves rider 2 as trip 2. ```hint Ranking vehicles You only need to rank vehicles by distance, never report the distance itself. Look for a way to compare exactly with integers, and make the smallest-ID rule part of the same comparison so the result does not depend on iteration order. ``` ```hint State that moves together List every piece of state each call touches: the trip's status, the vehicle's position, the vehicle's availability, and the rider's active trip. Check the current status first and change nothing when the call is invalid. ``` ### Constraints & Assumptions - `1 <= rider_id, vehicle_id <= 10^4` - Coordinates are integers from -10,000 to 10,000. - At most 10,000 calls in total across all methods. - IDs are unique within their own category (riders, vehicles). - Trip IDs are generated automatically, starting from 1. A request that returns -1 does not use up a trip ID. ### Clarifying Questions to Ask - When a trip completes, should the rider's stored location move to the destination, so a later request from that rider is matched from there? - Is a linear scan over the fleet acceptable at this scale, or should matching be designed for a much larger fleet? - Can trips be cancelled, or can vehicles go offline or be removed from the service? - Will calls arrive one at a time, or can several requests be processed concurrently? ### What a Strong Answer Covers - A class breakdown (rider, vehicle, trip, trip status, and the service) where it is clear which object owns availability and the active-trip link. - Matching that applies the nearest-vehicle rule and the smallest-ID tie-break exactly, with no floating-point comparison. - A trip state machine that rejects invalid transitions and returns the specified sentinel values without side effects. - Consistent updates of vehicle position, vehicle availability and the rider's active trip on every transition. - Time and space complexity per method, and how matching would scale beyond a linear scan. ### Follow-up Questions - How would you support cancelling a trip, and what should happen to the vehicle and rider in each status? - With a million vehicles, how would you find the nearest available one without scanning the whole fleet? - What changes if `request_ride` can be called from many threads at once, and how do you stop two riders from getting the same vehicle? - How would you extend the design to shared rides, where a vehicle has a seat capacity and can carry several riders?

Overview: Design the classes for a ride-sharing service that registers riders and autonomous vehicles, matches each ride request to the nearest available vehicle, and moves trips through requested, in-progress and completed states. It tests object modeling, state machine design and careful handling of ties and invalid calls.

Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design the internal class structure for a ride-sharing service that manages riders, autonomous vehicles, and trips. The service matches a rider to the nearest available vehicle using Euclidean distance and supports real-time trip status updates.

When a ride is requested, the system finds the closest available vehicle to the rider's current location. If several vehicles are equally close, the one with the smallest vehicle ID is selected. A vehicle can serve only one trip at a time, and a rider can have only one active trip at a time.

Trips move through three statuses: REQUESTED (matched but not started), IN_PROGRESS (vehicle en route or carrying the rider), and COMPLETED (rider delivered to the destination). When a trip starts, the vehicle moves to the pickup location. When a trip completes, the vehicle moves to the destination and becomes available again.

Implement the following methods:

  • add_rider(rider_id: int, x: int, y: int) -> None : register a new rider at location (x, y).
  • add_vehicle(vehicle_id: int, x: int, y: int) -> None : register a new autonomous vehicle at location (x, y).
  • request_ride(rider_id: int, dest_x: int, dest_y: int) -> int : match the rider to the nearest available vehicle and create a trip. Return the trip ID on success, or -1 if no vehicle is available, the rider already has an active trip, or the rider does not exist.
  • start_trip(trip_id: int) -> str : move the trip from REQUESTED to IN_PROGRESS . The vehicle moves to the pickup location. Return the new status string, or "INVALID" if the trip does not exist or is not in REQUESTED status.
  • complete_trip(trip_id: int) -> str : move the trip from IN_PROGRESS to COMPLETED . The vehicle moves to the destination and becomes available again. Return the new status string, or "INVALID" if the trip does not exist or is not in IN_PROGRESS status.
  • get_trip_status(trip_id: int) -> str : return the trip's current status: REQUESTED , IN_PROGRESS , COMPLETED , or NOT_FOUND .
  • get_vehicle_location(vehicle_id: int) -> List[int] : return the vehicle's current [x, y] , or [-1, -1] if the vehicle does not exist.

Example 1 shows a distance tie and the full trip lifecycle:

add_rider(1, 0, 0)
add_vehicle(10, 3, 4)
add_vehicle(7, -4, 3)
add_vehicle(5, 6, 8)
request_ride(1, 10, 10)    -> 1
get_trip_status(1)         -> "REQUESTED"
request_ride(1, 2, 2)      -> -1
get_vehicle_location(7)    -> [-4, 3]
start_trip(1)              -> "IN_PROGRESS"
get_vehicle_location(7)    -> [0, 0]
start_trip(1)              -> "INVALID"
complete_trip(1)           -> "COMPLETED"
get_vehicle_location(7)    -> [10, 10]
get_trip_status(2)         -> "NOT_FOUND"

Vehicles 10 and 7 are both at distance 5 from the rider and vehicle 5 is at distance 10, so the smaller ID, 7, gets trip 1. The second request fails because rider 1 already has an active trip. Vehicle 7 stays where it is until the trip starts, then moves to the pickup point (0, 0), and ends at the destination (10, 10).

Example 2 shows a busy fleet and invalid calls:

add_rider(1, 0, 0)
add_rider(2, 5, 5)
add_vehicle(3, 1, 1)
request_ride(1, 9, 9)      -> 1
request_ride(2, 0, 0)      -> -1
request_ride(4, 0, 0)      -> -1
complete_trip(1)           -> "INVALID"
start_trip(1)              -> "IN_PROGRESS"
complete_trip(1)           -> "COMPLETED"
request_ride(2, 0, 0)      -> 2
get_vehicle_location(8)    -> [-1, -1]

Rider 2's first request fails because the only vehicle is busy, and rider 4 was never registered. Trip 1 cannot complete before it starts. Once it completes, vehicle 3 is free again at (9, 9) and serves rider 2 as trip 2.

Constraints & Assumptions

  • 1 <= rider_id, vehicle_id <= 10^4
  • Coordinates are integers from -10,000 to 10,000.
  • At most 10,000 calls in total across all methods.
  • IDs are unique within their own category (riders, vehicles).
  • Trip IDs are generated automatically, starting from 1. A request that returns -1 does not use up a trip ID.

Clarifying Questions to Ask Guidance

  • When a trip completes, should the rider's stored location move to the destination, so a later request from that rider is matched from there?
  • Is a linear scan over the fleet acceptable at this scale, or should matching be designed for a much larger fleet?
  • Can trips be cancelled, or can vehicles go offline or be removed from the service?
  • Will calls arrive one at a time, or can several requests be processed concurrently?

What a Strong Answer Covers Guidance

  • A class breakdown (rider, vehicle, trip, trip status, and the service) where it is clear which object owns availability and the active-trip link.
  • Matching that applies the nearest-vehicle rule and the smallest-ID tie-break exactly, with no floating-point comparison.
  • A trip state machine that rejects invalid transitions and returns the specified sentinel values without side effects.
  • Consistent updates of vehicle position, vehicle availability and the rider's active trip on every transition.
  • Time and space complexity per method, and how matching would scale beyond a linear scan.

Follow-up Questions Guidance

  • How would you support cancelling a trip, and what should happen to the vehicle and rider in each status?
  • With a million vehicles, how would you find the nearest available one without scanning the whole fleet?
  • What changes if request_ride can be called from many threads at once, and how do you stop two riders from getting the same vehicle?
  • How would you extend the design to shared rides, where a vehicle has a seat capacity and can carry several riders?