KatInterview

Random Equipment Lending Pool with Constant-Time Operations

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-maprandomizedswap-and-pop

Last updated

Build an in-memory pool that lends out a uniformly random free item to a borrower, accepts returns, and allows retirement of idle items, with every operation in O(1) average time.

Fair-Rotation Equipment Pool

A security team keeps a fleet of wearable recording devices. At the beginning of a shift each staff member borrows one device, and at the end of the shift the device is returned. To spread usage evenly across the fleet, the system must hand out a uniformly random free device each time someone asks for one.

Implement a class DevicePool with the following methods:

  • add_device(device_id: str) -> bool: Adds a new device to the fleet. It begins in the free pool. Returns True if added, False if that device_id is already known (whether free or lent out).
  • retire_device(device_id: str) -> bool: Permanently deletes a device from the fleet. Only a free device can be deleted. Returns True on success, and False if the device is unknown or currently lent out.
  • lend_random(staff_id: str) -> Optional[str]: Gives staff_id a free device chosen uniformly at random and returns its ID. Each person may hold at most one device at once. Returns None if no device is free or if the person already holds one. A rejected request must not change any state.
  • return_device(device_id: str) -> bool: Puts a lent device back into the free pool. Returns True on success, and False if the device is unknown or is not currently lent out.
  • current_device(staff_id: str) -> Optional[str]: Returns the ID of the device staff_id currently holds, or None if they hold none.

Every operation must run in O(1) average time.

Constraints

  • 1 <= len(device_id), len(staff_id) <= 64
  • IDs are alphanumeric (the hyphenated IDs in the example are for readability).
  • At most 100,000 calls in total across all methods.
  • Every free device must be equally likely to be chosen by lend_random.

Example

pool = DevicePool()

pool.add_device("D1")        # True
pool.add_device("D2")        # True
pool.add_device("D3")        # True
pool.add_device("D2")        # False (already known)

d = pool.lend_random("alice")   # one of D1, D2, D3 with probability 1/3 each; say "D2"
pool.current_device("alice")    # "D2"
pool.lend_random("alice")       # None (alice already holds a device)
pool.retire_device("D2")        # False (currently lent out)

pool.return_device("D2")        # True
pool.return_device("D2")        # False (already free)
pool.retire_device("D2")        # True
pool.return_device("D9")        # False (never added)

What to cover

Describe your data structures, explain why they give O(1) time and uniform selection, handle the edge cases (empty pool, repeat borrowers, unknown or already-free devices, removing the last element of the internal array), and provide working code.

Hints

Hint 1

A hash set gives O(1) membership and deletion but cannot pick a random member in O(1). A plain array gives O(1) random indexing but deleting from the middle is O(n). Can you combine the two?

Hint 2

Keep free devices in a list, plus a dictionary from device ID to its position in that list. To delete an item at position i, overwrite it with the last list element, update that element's recorded position, then pop the tail.

Hint 3

Track lending with two dictionaries (device to holder, holder to device). Validate everything, including whether the person already holds a device and whether the pool is empty, before you modify any structure. Be careful when the element you delete is the last one in the list.