KatInterview

Parcel Locker Manager with History Tracking

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designheapsimulationdata-structure-design

Last updated

Build a locker allocation system that assigns the smallest free locker, validates pickups, and remembers the last package stored in each locker.

You are building a parcel locker controller for a delivery service. The system manages n lockers numbered from 1 to n.

Write a function that simulates a sequence of operations on the locker controller:

def run_locker_operations(n: int, operations: List[str], arguments: List[List]) -> List:

The function receives:

  • n: the total number of lockers (numbered 1 through n)
  • operations: a list of operation names
  • arguments: a parallel list where arguments[i] contains the parameters for operations[i]

Each operation produces one output value, and the function returns the list of all outputs in order.

Operations

"store" with argument [package_id]:

  • Assign the smallest available locker number to hold package_id
  • Return the assigned locker number
  • If package_id is already stored in some locker, or if all lockers are full, return -1 instead

"pickup" with arguments [locker_id, package_id]:

  • Release locker locker_id if it is currently occupied by package_id
  • Return True if the pickup succeeds and the locker is released
  • Return False if locker_id is out of range, the locker is empty, or the stored package does not match package_id
  • A released locker becomes immediately available for future store operations

"get_last_stored" with argument [locker_id]:

  • Return the package_id of the most recent package that was stored in locker_id, even if that locker is now empty
  • Return None if locker_id is out of range or if no package has ever been stored in that locker

Constraints

  • 1 ≤ n ≤ 100,000
  • 1 ≤ len(package_id) ≤ 32, alphanumeric characters only
  • 1 ≤ len(operations) ≤ 200,000
  • Each operation name is one of "store", "pickup", or "get_last_stored"
  • locker_id values in arguments may be out of range or invalid types

Examples

Example 1

Input: run_locker_operations(3, ['store', 'store', 'store', 'pickup', 'pickup', 'store', 'get_last_stored', 'pickup', 'get_last_stored', 'get_last_stored'], [['PKG-A'], ['PKG-B'], ['PKG-A'], [1, 'PKG-A'], [1, 'PKG-A'], ['PKG-C'], [1], [1, 'PKG-C'], [1], [3]])

Output: [1, 2, -1, True, False, 1, 'PKG-C', True, 'PKG-C', None]

Hints

Hint 1

Use a min-heap to efficiently track and retrieve the smallest available locker number.

Hint 2

Maintain separate data structures for current occupancy (which packages are in lockers right now) and historical data (what was the last package in each locker).

Hint 3

Track a reverse mapping from package IDs to locker numbers so you can quickly check if a package is already stored somewhere.