Problemparcel locker allocation network

Session: Sign in to solve

Solution.txt

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. storeParcel selects 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, storeParcel may choose any one of them.
  • Create one stored assignment. A successful storeParcel call 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, storeParcel returns "".
  • Preserve state after failed storage. A storeParcel call that returns "" changes no locker, parcel, or pickup-code assignment.
  • Pick up with the code. pickupParcel with a code assigned to a stored parcel returns and removes that parcel and makes its locker free.
  • Report an inactive code. pickupParcel with an unknown or already used code returns "".
  • Preserve state after failed pickup. A pickupParcel call that returns "" changes no locker, parcel, or pickup-code assignment.

API

SignatureReturnsBehavior
ParcelLockerService(smallLockerIds: string[], mediumLockerIds: string[], largeLockerIds: string[])Not applicableCreates each listed locker as free with the size of its constructor list.
storeParcel(parcelId: string, parcelSize: string, pickupCode: string)stringStores one parcel by the optimal-size rule and returns the selected locker ID, or returns "" when storage is unavailable.
pickupParcel(pickupCode: string)stringUses 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"]).

StepOperationResult
1storeParcel("P-100", "S", "CODE-100")"S-01"
2storeParcel("P-200", "S", "CODE-200")"M-01"
3pickupParcel("CODE-100")"P-100"
4pickupParcel("CODE-100")""
5storeParcel("P-300", "S", "CODE-300")"S-01"

Constraints

  • The three locker ID arrays are non-null and contain between 1 and 200000 IDs in total.
  • Locker IDs are unique across all three arrays, and every locker ID has between 1 and 32 characters.
  • Every parcel ID has between 1 and 32 characters and occurs in at most one storeParcel call per testcase.
  • Every pickup code has between 1 and 32 characters and occurs in at most one storeParcel call per testcase.
  • parcelSize is a string equal to "S", "M", or "L".
  • At most 200000 public 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.

PRIVATE WORKSPACE

Checking your session…

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