Durable Work Queue Operations
Problem statement
Simulate a durable work queue over a finite ordered sequence of operations. For operation i, use operations[i], taskIds[i], and timestamps[i]. Timestamps are nondecreasing.
Before processing each operation at time t, expire every active reservation whose deadline is at most t. A successful reservation at time t has deadline t + leaseDuration. Each successful reservation consumes one attempt.
Operations and results
ADD: add the unique nonempty task ID to the tail of the available queue and returnADDED.RESERVE: reserve the oldest available task, increment its attempt count, and return its ID. ReturnNONEwhen no task is available. The corresponding task ID input is ignored.COMPLETE: if the named task currently has an active reservation, mark it completed and returnCOMPLETED; otherwise returnIGNORED.FAIL: if the named task currently has an active reservation, either append it to the available tail and returnREQUEUED, or mark it dead and returnDEADwhen it has already consumedmaxAttempts. An invalid target returnsIGNORED.STATUS: returnAVAILABLE,RESERVED,COMPLETED, orDEADfor the named task, orMISSINGwhen it has never been added.
A timeout follows the same retry rule as FAIL, but produces no separate output. Retried tasks join the available tail. If multiple reservations expire at the same deadline, process them in the order in which they were reserved.
Return one result string for every input operation, in the same order.
Function
runWorkQueue(operations: String[], taskIds: String[], timestamps: int[], leaseDuration: int, maxAttempts: int) → String[]Examples
Example 1
operations = ["ADD","ADD","RESERVE","FAIL","RESERVE","COMPLETE","RESERVE"]taskIds = ["a","b","","a","","b",""]timestamps = [0,0,1,2,2,3,4]leaseDuration = 3maxAttempts = 2return = ["ADDED","ADDED","a","REQUEUED","b","COMPLETED","a"]Task a is reserved first, then its failure moves it behind b. The next two reservations therefore return b and then a.
Example 2
operations = ["ADD","RESERVE","FAIL","RESERVE","FAIL","STATUS"]taskIds = ["x","","x","","x","x"]timestamps = [0,1,2,2,3,3]leaseDuration = 5maxAttempts = 2return = ["ADDED","x","REQUEUED","x","DEAD","DEAD"]The second reservation consumes task x's final allowed attempt. Its next failure sends it to dead letter, which STATUS exposes as DEAD.
Constraints
1 <= operations.length <= 200000.operations.length == taskIds.length == timestamps.length.- Each operation is
ADD,RESERVE,COMPLETE,FAIL, orSTATUS. 0 <= timestamps[i] <= 10^9, and timestamps are nondecreasing.1 <= leaseDuration <= 10^9and1 <= maxAttempts <= 100000.- Every
ADDuses a unique nonempty printable ASCII task ID of length at most50.