#3829
Design Ride Sharing System
specialist · 675 · lc medium +30 · 63.3% accepted · 45 likes · top 66%
Description
A ride-sharing platform tracks pending ride requests and available drivers, matching them in arrival order. Implement the RideSharingSystem class:
- RideSharingSystem() — initializes the system.
- void addRider(int riderId) — registers a rider waiting for a match.
- void addDriver(int driverId) — registers a driver available for a match.
- int[] matchDriverWithRider() — pairs the earliest-available driver with the earliest-waiting rider, removes both, and returns [driverId, riderId]. Returns [-1, -1] if no match can be made.
- void cancelRider(int riderId) — removes the rider from the queue if still unmatched.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24