Archive Box Circulation
Problem
Design ArchiveBoxCirculation for a library network that lends physical archive boxes to researchers. Each box belongs to one branch when available, remains associated with that branch while borrowed, and may be returned to a different branch. Calls are sequential on one shared instance.
Requirements
- Initialize inventory. Construction assigns box
boxIds[i]the state(initialBranchIds[i], available). - Borrow a box.
borrow(boxId)changes that box from(branchId, available)to(branchId, borrowed). - Return a box.
returnBox(boxId, branchId)changes that box from(previousBranchId, borrowed)to(branchId, available). - Observe availability.
isAvailable(boxId, branchId)returns whether that box's state is exactly(branchId, available).
API
| Signature | Returns | Behavior |
|---|---|---|
ArchiveBoxCirculation(branchIds: string[], boxIds: string[], initialBranchIds: string[]) | Not applicable | Performs the initialization transition. |
borrow(boxId: string) | void | Performs the borrow transition. |
returnBox(boxId: string, branchId: string) | void | Performs the return transition. |
isAvailable(boxId: string, branchId: string) | boolean | Evaluates the availability query. |
Examples
Starting with branches ["north", "south"], boxes ["BX-1"], and initial branches ["north"]:
| Step | Operation | Result |
|---|---|---|
| 1 | isAvailable("BX-1", "north") | true |
| 2 | borrow("BX-1") | No return value |
| 3 | isAvailable("BX-1", "north") | false |
| 4 | returnBox("BX-1", "south") | No return value |
| 5 | isAvailable("BX-1", "south") | true |
Constraints
1 <= branchIds.length <= 101 <= boxIds.length <= 20- A testcase contains at most 50 method calls.
- Every ID matches
[A-Za-z0-9_-]{1,32}. branchIdsandboxIdscontain unique values.boxIdsandinitialBranchIdshave equal lengths.- Every referenced branch is present in
branchIds. - Every method argument is nonempty and names a configured box or branch.
- Every
borrownames an available box. - Every
returnBoxnames a borrowed box.
Notes
All calls satisfy the constraints. Behavior outside these preconditions is unspecified and is not judged.