Relief Courier Queue
Problem
A relief organization hires local couriers for individual deliveries. Design ReliefCourierQueue to record each available courier's quoted fee and dispatch the lowest-cost available courier.
Requirements
- Empty start. Construction starts with no available couriers.
- Post courier.
postCourieradds the courier ID and fee to the available couriers. - Fee-priority dispatch.
dispatchremoves and returns the available courier whose fee is smallest. - Availability lookup.
isAvailablereturns whether the courier ID is present.
API
| Signature | Returns | Behavior |
|---|---|---|
ReliefCourierQueue() | Not applicable | Creates an empty queue. |
postCourier(courierId: string, fee: integer) | void | Adds the courier and its fee. |
dispatch() | string | Removes and returns the lowest-fee available courier. |
isAvailable(courierId: string) | boolean | Reports current availability. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | ReliefCourierQueue() | Queue created |
| 2 | postCourier("RIDGE", 70) | No return value |
| 3 | postCourier("VALLEY", 20) | No return value |
| 4 | dispatch() | "VALLEY" |
| 5 | isAvailable("VALLEY") | false |
| 6 | isAvailable("RIDGE") | true |
Constraints
- At most 12 couriers are posted and at most 30 public method calls occur.
- Courier IDs match
[A-Za-z0-9_-]{1,24}. 1 <= fee <= 1000.- Courier IDs and fees are unique.
dispatchis called only when at least one courier is available.- Arguments are not null.
Notes
Calls are sequential.