KatInterview

Simulate Driver Availability and Task Allocation

Data Structure DesignAuto-graded · 22 test cases
EasyAuto-gradedData Structure Designdata-structuresqueueshash-table

Last updated

Simulate driver registration and dispatch operations using standard queue processing and state tracking.

Design a data structure simulation that manages driver availability and task dispatching.

You are given a sequence of instructions specified by two aligned lists: operations and arguments.

The system supports two commands:

  1. "add": Takes a single integer driver ID (args[0]).

    • Registers the driver as available if they are not already in the system (neither currently registered as available nor already dispatched).
    • If the driver ID is already present in either state, the operation is ignored (idempotent).
    • Returns None.
  2. "dispatch": Takes an empty list args.

    • Allocates a task to the available driver who was registered earliest (FIFO order).
    • Moves that driver from available to dispatched state.
    • Returns the ID of the dispatched driver.
    • If no drivers are currently available, returns None.

Process the list of operations sequentially and return a list containing the output of each command in order.

Constraints

  • operations and arguments have the same length $N$, where $1 \le N \le 10^5$.
  • operations[i] is either "add" or "dispatch".
  • For "add", arguments[i] contains exactly one integer representing a driver ID ($0 \le \text{ID} \le 10^9$).
  • For "dispatch", arguments[i] is an empty list [].
  • Operations must be processed in the exact sequence provided.

Examples

Example 1

Input: process_rider_operations(['add', 'add', 'dispatch', 'dispatch'], [[7], [7], [], []])

Output: [None, None, 7, None]

Example 2

Input: process_rider_operations(['dispatch', 'add', 'add', 'dispatch'], [[], [12], [12], []])

Output: [None, None, None, 12]

Hints

Hint 1

Maintain separate collections to track the FIFO queue of available drivers, a hash set of available drivers, and a hash set of dispatched drivers.

Hint 2

Before adding a driver, verify they are neither in the available set nor the dispatched set.

Hint 3

When dispatching, extract the first driver from the available queue, update the sets accordingly, and return the driver's ID.