Parcel Locker Service
Problem
Design ParcelLockerService for a pickup location with small (S), medium (M), and large (L) lockers. It places each parcel in the smallest available size that fits and releases the locker when the parcel is collected with its pickup code.
Requirements
- Choose the optimal size.
storeParcelselects the smallest locker size that can hold the parcel and currently has a free locker. - Allow any locker in the size. When several lockers are free in the selected size,
storeParcelmay choose any one of them. - Create one stored assignment. A successful
storeParcelcall associates the parcel and pickup code with one previously free locker, leaves every other locker unchanged, and returns that locker ID; each locker holds at most one parcel. - Report unavailable capacity. If no compatible locker is free,
storeParcelreturns"". - Preserve state after failed storage. A
storeParcelcall that returns""changes no locker, parcel, or pickup-code assignment. - Pick up with the code.
pickupParcelwith a code assigned to a stored parcel returns and removes that parcel and makes its locker free. - Report an inactive code.
pickupParcelwith an unknown or already used code returns"". - Preserve state after failed pickup. A
pickupParcelcall that returns""changes no locker, parcel, or pickup-code assignment.
API
| Signature | Returns | Behavior |
|---|---|---|
ParcelLockerService(smallLockerIds: string[], mediumLockerIds: string[], largeLockerIds: string[]) | Not applicable | Creates each listed locker as free with the size of its constructor list. |
storeParcel(parcelId: string, parcelSize: string, pickupCode: string) | string | Stores one parcel by the optimal-size rule and returns the selected locker ID, or returns "" when storage is unavailable. |
pickupParcel(pickupCode: string) | string | Uses a current code to pick up and return its parcel ID, or returns "" for an inactive code. |
Examples
Create ParcelLockerService(["S-01"], ["M-01"], ["L-01"]).
| Step | Operation | Result |
|---|---|---|
| 1 | storeParcel("P-100", "S", "CODE-100") | "S-01" |
| 2 | storeParcel("P-200", "S", "CODE-200") | "M-01" |
| 3 | pickupParcel("CODE-100") | "P-100" |
| 4 | pickupParcel("CODE-100") | "" |
| 5 | storeParcel("P-300", "S", "CODE-300") | "S-01" |
Constraints
- The three locker ID arrays are non-null and contain between
1and200000IDs in total. - Locker IDs are unique across all three arrays, and every locker ID has between
1and32characters. - Every parcel ID has between
1and32characters and occurs in at most onestoreParcelcall per testcase. - Every pickup code has between
1and32characters and occurs in at most onestoreParcelcall per testcase. parcelSizeis a string equal to"S","M", or"L".- At most
200000public method calls occur per testcase.
Notes
Calls are sequential. Inputs satisfy the constraints; an unknown or already used pickup code is a valid argument to pickupParcel.