FastPrepDurable Work Queue Operations

Durable Work Queue Operations

OpenAI logoOpenAI● HardFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

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 return ADDED.
  • RESERVE: reserve the oldest available task, increment its attempt count, and return its ID. Return NONE when 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 return COMPLETED; otherwise return IGNORED.
  • FAIL: if the named task currently has an active reservation, either append it to the available tail and return REQUEUED, or mark it dead and return DEAD when it has already consumed maxAttempts. An invalid target returns IGNORED.
  • STATUS: return AVAILABLE, RESERVED, COMPLETED, or DEAD for the named task, or MISSING when 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, or STATUS.
  • 0 <= timestamps[i] <= 10^9, and timestamps are nondecreasing.
  • 1 <= leaseDuration <= 10^9 and 1 <= maxAttempts <= 100000.
  • Every ADD uses a unique nonempty printable ASCII task ID of length at most 50.

More OpenAI problems

See OpenAI hiring insights
public String[] runWorkQueue(String[] operations, String[] taskIds, int[] timestamps, int leaseDuration, int maxAttempts) {
    // Write your code here.
}
operations["ADD","ADD","RESERVE","FAIL","RESERVE","COMPLETE","RESERVE"]
taskIds["a","b","","a","","b",""]
timestamps[0,0,1,2,2,3,4]
leaseDuration3
maxAttempts2
expected["ADDED", "ADDED", "a", "REQUEUED", "b", "COMPLETED", "a"]
Checking account…