Locker Allocation System
Parcel Delivery Lockers
Design the software behind a bank of parcel lockers: place each package in the smallest free locker that fits, hand the recipient a one-time code, and take the locker back when the package is collected or left too long.
Overview
A locker bank stands in a store or an apartment lobby. A courier arrives with a package, the system opens a locker that is big enough, and the recipient later opens the same locker with a code. Your job is the software in the middle: choosing the locker, generating the code, checking it on pickup, and freeing lockers that nobody collects from.
The problem looks small but touches the parts of design that interviewers care about. There is a resource pool with sizes, so an allocation rule matters. There is a lifecycle per locker, so state matters. Many couriers can arrive at once, so the pool needs a lock. And the code is a secret, so it has to be generated and compared like one.
A strong answer names the allocation rule in the first minute (best fit: the smallest free locker that fits), draws the locker states, and writes the pool with one lock around every change. It also says what happens when a package is never collected, because that is where most first drafts leak lockers.
Scoping the problem
Ask these six questions before you draw anything. Each answer changes the design, and the one assumed in this chapter follows it.
- How many sizes are there, and does a big locker take a small package? This decides whether allocation is a lookup or a search. Assume three sizes, and any locker takes a package of its size or smaller.
- Which locker wins when several fit? Best fit keeps big lockers free for big packages, so use best fit, as a swappable policy.
- How does the recipient open the locker? A code needs secure generation and a limit on guesses. Assume a six-digit code with five wrong tries allowed.
- How long does a package wait? This sets when a locker is reclaimed and who clears it. Assume a fixed hold time, after which a courier takes expired packages back.
- Can several couriers use the bank at the same time? Yes, and two couriers must never get the same locker, so the bank has one lock.
- Do we notify the recipient? That adds an observer hook, while the sending itself is outside the system. Keep a callback list that the app or an SMS gateway subscribes to.
Functional requirements
- allocate(package) places the package in the smallest free locker that fits and returns the locker and a one-time access code, or nothing when no locker fits
- pickup(package_id, code) opens the locker only for the right code, then frees the locker
- A wrong code counts as a failed attempt; after five, the locker refuses every code until a courier clears it
- expire_overdue() marks lockers whose hold time has passed as expired and refuses pickups from them
- courier_clears(locker_id) returns an expired locker to empty once the courier has taken the package back
- Interested parties can subscribe to be told when a package is delivered
Non-functional requirements
- Two couriers can never be given the same locker: every state change happens under one lock
- Access codes come from a cryptographic random source and are compared in constant time
- The allocation rule is a swappable policy object; adding a rule does not change the bank
- The clock is injected, so expiry can be tested without waiting
Interview questions
- Why best fit rather than first fit? Is the extra work worth it?
- A customer orders three items that arrive as three packages. How do you give them one code?
- Who notices that a package has been sitting for three days: a background job or the next allocation?
- Two couriers arrive at the same time at two banks in different cities. Does one lock still work?
- How do you stop someone from guessing a code?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio