Problemmulti branch pharmacy fulfilment

Session: Sign in to solve

Solution.txt

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. receiveStock increases the available quantity of the named medicine at the named branch by exactly units.
  • Follow route priority. reserve selects the first branch in constructor order with at least one available unit of the requested medicine.
  • Hold one unit. A successful reserve decreases that branch's available quantity by one and records one active reservation under reservationId.
  • Leave failed searches unchanged. If no branch has an available unit, reserve returns null without changing any branch-medicine quantity.
  • Cancel a hold. cancel restores exactly one unit of the reserved medicine to the branch that supplied it.

API

Reservation has string fields reservationId, medicineId, and branchId.

SignatureReturnsBehavior
PharmacyFulfilment(branchIds: string[])Not applicableCreates zero inventory for each distinct branch and fixes search priority to array order.
receiveStock(branchId: string, medicineId: string, units: integer)voidApplies Receive stock.
reserve(reservationId: string, medicineId: string)Reservation or nullApplies Follow route priority, Hold one unit, and Leave failed searches unchanged.
cancel(reservationId: string)voidApplies Cancel a hold.

Examples

Starting with branches ["area", "additional", "main"]:

StepOperationResult
1receiveStock("main", "aspirin", 2)No return value
2receiveStock("additional", "aspirin", 1)No return value
3reserve("R-1", "aspirin"){reservationId: "R-1", medicineId: "aspirin", branchId: "additional"}
4reserve("R-2", "aspirin"){reservationId: "R-2", medicineId: "aspirin", branchId: "main"}
5cancel("R-1")No return value
6reserve("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.
  • branchIds contains 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 receiveStock branch belongs to the constructor route.
  • Each reservationId passed to reserve has never appeared in an earlier reserve call, including a failed call.
  • Each cancel is called exactly once for an active successful reservation.

Notes

All calls satisfy the constraints. Behavior outside these preconditions is unspecified and is not judged.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.