Assign Locker
Problem statement
An automatic locker system is introduced into the changing room. When a customer visits the changing room, the system works as follows:
- If a customer has not been assigned a locker, the system assigns them the lowest available locker number;
- If a customer has already been assigned a locker, the system opens and releases that locker. Afterwards, the locker can be reassigned to another customer.
The locker numbers start from 1. At the beginning of the day, all lockers are empty. The changing room is visited N times. Which locker is assigned to a customer for the last time?
Historical occupancy follow-up
How would you extend the system to retrieve lockers that were occupied during a requested time span? Record the assignment and release times for each occupancy interval, then explain how to index that history for repeated time-range queries.
Examples
Example 1
clients = ["Alice", "Eve", "Bob", "Eve", "Carl", "Alice"]return = 2- Locker 1 is assigned to Alice;
- Locker 2 is assigned to Eve;
- Locker 3 is assigned to Bob;
- Eve releases locker 2;
- Locker 2 is assigned to Carl;
- Alice releases locker 1.
The last assigned locker is locker 2, so the function should return 2.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- Worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.