Simulate Driver Availability and Task Allocation
Data Structure DesignAuto-graded · 22 test casesLast 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:
-
"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.
-
"dispatch": Takes an empty listargs.- 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
operationsandargumentshave 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To implement the system state efficiently:
- Maintain a FIFO queue (
collections.deque) to store driver IDs in the order they become available. - Maintain a set
available_setfor fast lookup of drivers currently waiting in the queue. - Maintain a set
assigned_setto record drivers who have already been dispatched. - Iterate through each command pair
(op, args)in order:"add": Check if the driver ID is inavailable_setorassigned_set. If not, add the ID toavailable_queueandavailable_set. RecordNonein the results."dispatch": Ifavailable_queueis not empty, dequeue the next driver, remove them fromavailable_set, add them toassigned_set, and record their ID in the results. If empty, recordNone.
Complexity
- Time Complexity: $\mathcal{O}(1)$ amortized per operation using
dequeand hash sets. Overall time complexity is $\mathcal{O}(N)$, where $N$ is the number of operations. - Space Complexity: $\mathcal{O}(U)$, where $U$ is the number of unique driver IDs stored across the sets and queue.
from typing import List, Optional
from collections import deque
def process_rider_operations(operations: List[str], arguments: List[List[int]]) -> List[Optional[int]]:
available_queue = deque()
available_set = set()
assigned_set = set()
results = []
for op, args in zip(operations, arguments):
if op == "add":
driver_id = args[0]
if driver_id not in available_set and driver_id not in assigned_set:
available_queue.append(driver_id)
available_set.add(driver_id)
results.append(None)
elif op == "dispatch":
if available_queue:
driver_id = available_queue.popleft()
available_set.remove(driver_id)
assigned_set.add(driver_id)
results.append(driver_id)
else:
results.append(None)
else:
raise ValueError(f"Unsupported operation: {op}")
return results