Design a Best-Fit Locker Assignment Component
Context
You are designing the locker-assignment component for a Locker OS used at physical locker banks (one or more locations). Lockers and packages have sizes: Small (S), Medium (M), Large (L). Each locker holds at most one package. Couriers arrive with packages and need to be assigned the best-fitting LockerID quickly and safely under concurrency.
Assume:
-
The component may run per location (preferred) or centrally with per-location sharding.
-
Locker devices report available/occupied states; the service is the source of truth for assignment decisions.
Requirements
Design a component that:
-
Returns the best-fitting locker for a given package size.
-
Defines a clear "best fit" policy (exact match preferred; upsizing rules and priorities).
-
Exposes core operations:
-
Register lockers
-
Mark locker available/occupied/broken
-
Assign locker to package (for courier)
-
Release on pickup
-
Query status and counts
-
Specifies data structures and time/space complexity.
-
Addresses concurrency (simultaneous couriers), idempotency and retries, race conditions, fairness/starvation.
-
Describes persistence and failure recovery.
-
Provides APIs, schema, and pseudocode for assign(packageSize) and release(lockerId).
-
Considers extensions: reservations, per-location sharding, priorities/SLA tiers, and scaling.