← 返回 doordash 的题目列表Implement and Debug a Random Dasher Picker
类型:online_judge
Random Dasher Picker (Debugging)
Implement a data structure for managing available delivery drivers (Dashers). Each Dasher has a unique integer dasherId.
Support the following operations:
ADD id: Add id to the available Dasher set.
Print false if it already exists; otherwise insert it and print true.
PICK: Select one currently available Dasher uniformly at random, print its ID, and remove it from the set.
Print EMPTY when the set is empty.
SIZE: Print the number of currently available Dashers.
Requirements:
Both ADD and a successful PICK must run in expected O(1) time.
The implementation must not throw on an empty set, a one-element set, or when the removed item is already the last item.
There are at most 2 * 10^5 operations, and each dasherId is a signed 32-bit integer.
Explain the race conditions that can occur if ADD and PICK run concurrently, and provide one solution that preserves correctness.
If Dasher availability must be confirmed by a slow upstream service, explain how to avoid indefinitely blocked requests and duplicate assignments.
Input Format
The first line contains integer q.
Each of the next q lines is one operation: ADD id, PICK, or SIZE.
Output Format
Print one result per operation.
Example
Input:
8
PICK
ADD 101
ADD 202
ADD 101
SIZE
PICK
SIZE
PICK
Output:
EMPTY
true
true
false
2
101 or 202
1
the other remaining ID
The actual PICK output is random; every available Dasher must have the same probability of being selected.
Example
Input
1
PICK
Output
EMPTY