← 返回 doordash 的题目列表Order Assignment with Round Robin and Consistent Hashing
类型:online_judge
Problem: Order Assignment with Round Robin and Consistent Hashing
DoorDash has multiple backends/workers and needs to assign orders to healthy workers.
Implement an order assigner with two modes.
Part 1: Round Robin
Supported operations:
ADD worker_id: add a worker. Ignore if it already exists.
REMOVE worker_id: remove a worker. Ignore if it does not exist.
NEXT: return the next available worker and advance the pointer. If no worker exists, return NONE.
Requirements:
Use workers in insertion order.
Removed workers must never be returned.
After removing a worker near the current pointer, the next NEXT should still return the correct worker.
Part 2: Consistent Hashing
Supported operations:
CADD worker_id: add a worker to the consistent hash ring.
CREMOVE worker_id: remove a worker from the ring.
GET order_id: return the worker assigned to this order. If the ring is empty, return NONE.
Requirements:
Each worker has V virtual nodes.
For GET order_id, hash the order id and find the first virtual node whose hash is greater than or equal to it; wrap around if needed.
Return the real worker for that virtual node.
Input Format
V q
command_1
...
command_q
Output Format
Print one line for every NEXT or GET operation.
Constraints
1 <= V <= 200
1 <= q <= 2 * 10^5
worker_id and order_id are strings without spaces.
Example
Input
1 8
NEXT
ADD a
ADD b
NEXT
NEXT
NEXT
REMOVE a
NEXT
Output
NONE
a
b
a
b