Katkat

Design body camera checkout system

AxonSoftware Engineer · Onsite

Last 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. Returns True if registered successfully, or False if device_id already 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). Returns True if removed successfully, or False if it does not exist or is currently checked out.
  • checkout_random(officer_id: str) -> Optional[str]: Assigns an available camera uniformly at random to officer_id. An officer can hold at most one camera at a time. Returns the assigned device_id, or None if 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. Returns True if successful, or False if the camera does not exist or was not currently checked out.
  • get_assigned_device(officer_id: str) -> Optional[str]: Returns the device_id currently assigned to officer_id, or None if 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,000 total 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)?