Multi-Branch Pharmacy Fulfilment
Problem
Design PharmacyFulfilment to receive medicine stock, reserve one unit from an ordered route of pharmacy branches, and cancel active reservations. Calls are sequential on one shared instance.
Requirements
- Receive stock.
receiveStockincreases the available quantity of the named medicine at the named branch by exactlyunits. - Follow route priority.
reserveselects the first branch in constructor order with at least one available unit of the requested medicine. - Hold one unit. A successful
reservedecreases that branch's available quantity by one and records one active reservation underreservationId. - Leave failed searches unchanged. If no branch has an available unit,
reservereturnsnullwithout changing any branch-medicine quantity. - Cancel a hold.
cancelrestores exactly one unit of the reserved medicine to the branch that supplied it.
API
Reservation has string fields reservationId, medicineId, and branchId.
| Signature | Returns | Behavior |
|---|---|---|
PharmacyFulfilment(branchIds: string[]) | Not applicable | Creates zero inventory for each distinct branch and fixes search priority to array order. |
receiveStock(branchId: string, medicineId: string, units: integer) | void | Applies Receive stock. |
reserve(reservationId: string, medicineId: string) | Reservation or null | Applies Follow route priority, Hold one unit, and Leave failed searches unchanged. |
cancel(reservationId: string) | void | Applies Cancel a hold. |
Examples
Starting with branches ["area", "additional", "main"]:
| Step | Operation | Result |
|---|---|---|
| 1 | receiveStock("main", "aspirin", 2) | No return value |
| 2 | receiveStock("additional", "aspirin", 1) | No return value |
| 3 | reserve("R-1", "aspirin") | {reservationId: "R-1", medicineId: "aspirin", branchId: "additional"} |
| 4 | reserve("R-2", "aspirin") | {reservationId: "R-2", medicineId: "aspirin", branchId: "main"} |
| 5 | cancel("R-1") | No return value |
| 6 | reserve("R-3", "aspirin") | {reservationId: "R-3", medicineId: "aspirin", branchId: "additional"} |
Constraints
1 <= branchIds.length <= 20- Each identifier has 1 through 64 ASCII letters, digits, periods, underscores, or hyphens.
branchIdscontains distinct values.1 <= units <= 1,000,000- At most 5,000 distinct medicines, 15,000 successful reservations, and 30,000 total method calls occur.
- Each branch-medicine available quantity and total received quantity fit a signed 64-bit integer.
- Every
receiveStockbranch belongs to the constructor route. - Each
reservationIdpassed toreservehas never appeared in an earlierreservecall, including a failed call. - Each
cancelis called exactly once for an active successful reservation.
Notes
All calls satisfy the constraints. Behavior outside these preconditions is unspecified and is not judged.