55A work queue that survives failures
55 A work queue that survives failures
OpenAI software engineer candidates report a fault-tolerant work queue as their coding question (3 reports). Two of them name the parts: enqueue, reserve, fail and a dead-letter queue, with unit tests given up front that all had to pass (2 reports). In both, a second part added dependencies between tasks. One candidate adds that a task comes with a cooldown after it is preempted, worked out from the preempt timestamp, and that a task put back in the queue keeps its place by its original enqueue time. That candidate missed the ordering rule, the tests did not all pass, and the round failed (1 report). Another solved part one, then ran out of time to debug part two (1 report). A prep guide's page on OpenAI mentions a queue with visibility timeouts (1 source). This is practice in that shape, not a leaked question.
Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/55_work_queue -k level1. Earlier levels must keep passing.
Build WorkQueue in solution.py. All time comes from clock, a function that returns the current time in seconds
(time.monotonic by default). Never read the system clock yourself: the tests pass a fake clock and move it by hand.
Workers are names like "w1". A task's status(task_id) is "waiting", "reserved", "done" or "dead", and
None for an id the queue has never accepted.
Level 1. Enqueue, reserve and ack. WorkQueue(max_attempts=3, visibility_timeout=None, clock=time.monotonic)
takes its arguments by keyword. enqueue(task_id) adds a waiting task. An id the queue has seen before, in any
state, raises ValueError. reserve(worker) hands that worker the waiting task enqueued earliest, marks it
reserved, and returns its id, or None when no task is waiting. A reserved task is hidden from other workers.
ack(worker, task_id) marks the task done and returns True, but only when that worker holds its reservation.
Anything else (another worker, a second ack, an unknown id) returns False and changes nothing.
Level 2. Fail, retries and a dead-letter queue. fail(worker, task_id) gives a reserved task back and returns
True, with the same holder rule as ack. The task waits again in its original place: ahead of every task
enqueued after it. Each failure counts as one failed attempt. When a task's failed attempts reach max_attempts, it
goes to the dead-letter queue instead and its status is "dead". dead_letters() returns the dead task ids in the
order they died. A max_attempts below 1 raises ValueError.
Level 3. Time. With a visibility_timeout, a reservation made at time t is a lease that is live while
t <= now < t + visibility_timeout. At t + visibility_timeout it ends by itself, and that counts as a failed
attempt at that moment, so it can send the task to the dead-letter queue. When several leases ended since the last
call, they end in the order of their deadlines. After that, ack and fail from the
old holder return False. visibility_timeout=None means a lease never ends. enqueue(task_id, cooldown=0): a
task that fails at time t, by fail or by a timeout, can't be reserved again before t + cooldown. A
visibility_timeout that isn't positive, or a negative cooldown, raises ValueError and stores nothing.
Level 4. Dependencies. enqueue(task_id, cooldown=0, depends_on=()). Every id in depends_on must already be
enqueued, or the call raises ValueError and stores nothing. A task can't be reserved until every task it depends
on is done, and a dependency that is done already doesn't hold it back. Once it's free, it takes its place by its own enqueue time. When a task goes to the dead-letter queue,
every task that depends on it, directly or through others, goes too, right after it, in enqueue order. A task
that is dead already stays where it is. A task
enqueued with a dependency that is already dead goes straight to the dead-letter queue.
Things to get right: the place a failed task goes back to (the detail one candidate lost the round on), the lease boundary, a timeout that nobody notices until the next call, and a worker whose lease ran out and who acks anyway. Follow-ups to be ready for: what happens when the worker finished the work but its ack arrived too late, how you'd make a task safe to run twice, how long a lease should be and how a slow worker keeps it, and what you'd do with the dead-letter queue once tasks land there.