Design body camera checkout system
AxonSoftware Engineer · OnsiteLast updated
Design an in-memory inventory management system for body-worn cameras that supports O(1) registration, uniform random checkout, and checkin operations.
Problem Statement
Law enforcement officers must check out body cameras at the start of their shift and return them at the end. To ensure balanced wear-and-tear across the fleet, the dispatch system should randomly assign any currently available camera to an officer upon request.
Implement the BodyCameraManager class, which manages a pool of camera devices and their assignments to officers:
register_device(device_id: str) -> bool: Registers a new camera in the system. The device starts in the available pool. ReturnsTrueif registered successfully, orFalseifdevice_idalready exists.unregister_device(device_id: str) -> bool: Permanently removes a camera from the system. A camera can only be removed if it is currently available (not checked out). ReturnsTrueif removed successfully, orFalseif it does not exist or is currently checked out.checkout_random(officer_id: str) -> Optional[str]: Assigns an available camera uniformly at random toofficer_id. An officer can hold at most one camera at a time. Returns the assigneddevice_id, orNoneif no cameras are available or the officer already has a camera.checkin_device(device_id: str) -> bool: Returns a checked-out camera back to the available pool. ReturnsTrueif successful, orFalseif the camera does not exist or was not currently checked out.get_assigned_device(officer_id: str) -> Optional[str]: Returns thedevice_idcurrently assigned toofficer_id, orNoneif the officer has no assigned device.
All operations must run in O(1) average time complexity.
Constraints
1 <= len(device_id), len(officer_id) <= 64- Up to
100,000total calls across all methods. - Camera IDs and Officer IDs contain only alphanumeric characters.
- Device assignment must be uniformly random among all currently available cameras.
Example
manager = BodyCameraManager()
manager.register_device("CAM-101") # returns True
manager.register_device("CAM-102") # returns True
manager.register_device("CAM-103") # returns True
# Randomly chooses CAM-101, CAM-102, or CAM-103 with equal probability
cam = manager.checkout_random("OFFICER-42") # e.g., returns "CAM-102"
manager.get_assigned_device("OFFICER-42") # returns "CAM-102"
manager.checkout_random("OFFICER-42") # returns None (officer already has a camera)
manager.unregister_device("CAM-102") # returns False (currently checked out)
manager.checkin_device("CAM-102") # returns True (now available again)
manager.unregister_device("CAM-102") # returns True (successfully removed)
Edge Cases to Consider
- Checking out when the available pool is empty.
- Checking in a device ID that was never registered or is already in the available pool.
- Officers attempting to check out multiple cameras concurrently.
Hints
Hint 1
To pick an element uniformly at random in O(1) time, storing available items in a dynamic array is standard.
Hint 2
To remove an arbitrary element from an array in O(1) time, swap it with the last element and pop, using a hash map to look up its index.
Hint 3
Maintain separate hash maps for officer-to-device assignments and device-to-officer mappings to ensure checkout/checkin validations remain O(1).
Follow-up questions
- Instead of uniform random selection, how would you modify the system to prioritize cameras with the lowest total recorded hours or highest battery percentage?
- How would you ensure thread-safety if multiple officers attempt to check out devices concurrently?
- How would you handle device maintenance quarantine (e.g., automatically moving broken devices out of the checkout pool after an officer flags an issue)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.