Stage 1 of 3: Reserve, fail, give up
Background. Background jobs sit in a queue until a worker takes one. Work sometimes fails, so the queue hands a failed task out again, up to a limit, and puts a task that keeps failing among its dead letters for a person to look at.
Implement RetryQueue(max_tries), enqueue(task_id, now), reserve(now), ack(task_id, now), fail(task_id, now) and dead_tasks() in work_queue.py.
A task the queue accepted is never lost: it is always waiting, reserved, completed or dead, and only one of these.
enqueueaccepts a new task, which waits. An id this queue has accepted before raisesValueError, whatever became of that task.reservetakes the waiting task that was enqueued first, makes it reserved and returns its id. When two were enqueued at the same time, the earlierenqueuecall goes first. It returnsNonewhen no task is waiting. A reserved task is handed to nobody else.ackfinishes a reserved task for good: it is then completed.failcounts one failure of a reserved task. Failure numbermax_triesof that task moves it to the dead letters, for good. Before that it waits again in its original place: its turn still goes by its enqueue, not by the failure.ackandfailraiseValueErrorwhen the task is not reserved: waiting, completed, dead or never accepted.dead_tasksreturns a new list of the dead tasks' ids in the order they got there. Changing that list changes nothing in the queue.- A call that raises changes nothing: it is no failure, and it doesn't use up an id.
Example
q = RetryQueue(max_tries=2)
q.enqueue("tide-table", 14)
q.enqueue("crew-list", 14)
q.reserve(15) # "tide-table": same time, earlier call
q.reserve(16) # "crew-list"
q.fail("crew-list", 18)
q.enqueue("menu-card", 19)
q.fail("tide-table", 21)
q.reserve(22) # "tide-table": still in first place
q.fail("tide-table", 23)
q.dead_tasks() # ["tide-table"]
q.reserve(24) # "crew-list": ahead of "menu-card"
Notes
max_triesis an integer from 1 to 100. Task ids are non-empty strings.nowis the time of the call, an integer from 0 to 10^12, and never less than thenowof an earlier call on the same queue, refused calls included.- Calls come one at a time. Several queues can be alive at once, and each holds only its own tasks.
- A test makes at most 2,000 calls and enqueues at most 300 tasks on one queue. No test measures speed, but every call has to return at these sizes: each test file has 18 seconds to run.
- On the starter, all 11 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
work_queue.py - Read-only: all
tests/test_*.pyfiles - Times are integers from 0 to 10^12 that never decrease from call to call on one queue; calls come one at a time
- A test makes at most 2,000 calls and enqueues at most 300 tasks on one queue; every call has to return at these sizes, with 18 seconds per test file
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Cooldown. A task that has just failed shouldn't run again at once, and it mustn't lose its turn or stall the others.
- Stage 3: Dependencies. Some tasks may start only after others are done, and none may be left waiting without end.
The files you get
- Yours to edit: work_queue.py.
- Read-only: tests/__init__.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
OpenAI runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Work Queue with Retries free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
OpenAI. It is modelled on OpenAI's coding round, which runs without AI. Based on candidates' reports of OpenAI interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
3 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, OpenAI's interviews, the AI coding interview guide.