Locker allocation system: assign the smallest empty locker that fits a package
Company: Amazon
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
A package pickup location has lockers of different sizes. Each locker is either empty or holds one package. When a package arrives, the system must pick the smallest empty locker whose size is at least the package size. Lockers can later be released, and the system can report whether a given locker is occupied.
Implement the following:
- `Locker(lockerId: str, size: int)`: initialize a locker. It starts empty.
- `Package(packageId: str, size: int)`: initialize a package.
- `PickupLocation(lockers: list[tuple[str, int]])`: initialize the location with a list of `(lockerId, size)` pairs. All lockers start empty.
- `findAndAllocate(packageId: str, packageSize: int) -> Optional[str]`: return the ID of the smallest empty locker whose size is at least `packageSize` and mark it occupied by that package, or return `None` if no empty locker is large enough.
- `releaseLocker(lockerId: str) -> bool`: if `lockerId` exists and is occupied, free it and return `True`; otherwise return `False`.
- `checkLockerStatus(lockerId: str) -> bool`: return `True` if `lockerId` exists and is occupied, otherwise `False`.
When several empty lockers share the smallest fitting size, return the one that was most recently freed or inserted. During initialization, a locker that appears later in the input list counts as inserted more recently.
Example (JSON-style output, so `null` stands for `None` and for the constructor call):
```text
Input:
["PickupLocation", "findAndAllocate", "findAndAllocate", "releaseLocker", "findAndAllocate", "checkLockerStatus"]
[[["L1", 10], ["L2", 10], ["L3", 20]], ["P100", 9], ["P101", 10], ["L2"], ["P102", 15], ["L3"]]
Output:
[null, "L2", "L1", true, "L3", true]
```
- All three lockers start empty: L1 and L2 have size 10, L3 has size 20.
- `findAndAllocate("P100", 9)` returns `"L2"`. The smallest size that fits is 10, and of L1 and L2, L2 was inserted more recently.
- `findAndAllocate("P101", 10)` returns `"L1"`, the only empty locker of size 10 left.
- `releaseLocker("L2")` returns `true`, and L2 is empty again.
- `findAndAllocate("P102", 15)` returns `"L3"`, because 20 is the only size of at least 15.
- `checkLockerStatus("L3")` returns `true`, since P102 is in it.
```hint Finding the fit
Every request asks for the smallest available size that is at least some value. Think about which ordered structure answers that query quickly, and what it must contain so that a size with no empty locker left is never returned.
```
```hint Breaking ties
The tie rule fixes the order in which empty lockers of one size are handed out. Pick a per-size container whose removal order matches that rule, then check it against the first call in the example.
```
### Constraints & Assumptions
- Between 1 and 10,000 lockers.
- Locker sizes and package sizes are integers from 1 to 10,000.
- Locker IDs and package IDs are non-empty alphanumeric strings.
- At most 10,000 calls in total to `findAndAllocate`, `releaseLocker` and `checkLockerStatus`.
### Clarifying Questions to Ask
- Are the locker IDs in the constructor input guaranteed to be unique?
- Can `findAndAllocate` be called with a `packageId` that is already in a locker, and should that call be rejected?
- Is the tie rule strict for testing, or may any empty locker of the smallest fitting size be returned?
- Can lockers be added or removed after the location is created?
- Do callers ever need to know which package is in a locker, or only whether it is occupied?
### What a Strong Answer Covers
- A data model that keeps a lookup from locker ID to locker (size and occupancy) separate from an index of empty lockers grouped by size.
- How the smallest fitting size is found faster than a scan of every locker, and how that index stays correct when a size runs out of empty lockers or gets one back.
- A per-size ordering that implements the most-recently-freed-or-inserted tie rule, checked against the example.
- Behavior for unknown IDs, releasing an empty locker, and a package that fits nowhere.
- Time and space complexity of each operation, stated for the structure actually used.
### Follow-up Questions
- How would you support adding and removing lockers at runtime, including a request to remove a locker that is occupied?
- Packages and lockers now have length, width and height, and a package may be rotated. How do the fit test and the search change?
- Several couriers call `findAndAllocate` at the same moment. How do you make sure two of them never get the same locker?
- How would you add pickup codes and automatically release lockers whose packages were not collected before a deadline?
Overview: Implement a locker allocator for a package pickup location that assigns the smallest empty locker that fits each package, breaks ties by the most recently freed locker, and supports release and status checks. It tests low-level design, the choice of ordered data structures, and honest per-operation complexity.