Bounded Worker Queue
Model a bounded FIFO work queue with explicit in-progress ownership and retry behavior. Items are non-null.
When workers fall behind, a queue has two jobs: keep new arrivals within a bound and preserve work that needs a retry. Build a small queue that makes the difference between waiting work and claimed work visible.
Implement a deterministic FIFO queue that tracks pending and in-progress work.
Requirements
- Define
WorkQueue(max_pending). - Items must be non-null (
Nonein Python ornullin Java). enqueue(item)appends pending work and returnsTrue; if pending capacity is full, returnFalse.start(worker_id)assigns the next pending item to an idle worker and returns it.- If there is no pending work,
start(worker_id)returnsNonewithout creating an active assignment. - If a worker already has in-progress work,
start(worker_id)returnsNone. complete(worker_id)removes and returns the worker's in-progress item.complete/failon an unknown or idleworker_idreturnNone(no-op on queue state).fail(worker_id, retry=True)removes the worker's item and optionally requeues it at the back.failreturns the removed item whether it is retried or dropped.- Capacity applies to
enqueueonly. Retry requeues even when pending is already atmax_pending(retry may temporarily makelen(pending) > max_pending). snapshot()returns{"pending": [...], "in_progress": {...}}.- Return independent snapshot collections: mutating a snapshot must not mutate the queue's internal state.
- Snapshot isolation applies to the returned list and dictionary, not to deep copies of item payloads. Items are opaque values; repeated equal values are separate work items.
Example
queue = WorkQueue(max_pending=2)
assert queue.enqueue("a")
assert queue.enqueue("b")
assert not queue.enqueue("c")
assert queue.start("w1") == "a"
assert queue.enqueue("c")
assert queue.complete("w1") == "a"
snapshot = queue.snapshot()
snapshot["pending"].clear()
assert queue.snapshot()["pending"] == ["b", "c"]Retry preserves accepted work even when new arrivals have filled the pending queue:
queue = WorkQueue(max_pending=1)
assert queue.enqueue("a")
assert queue.start("w1") == "a"
assert queue.enqueue("b")
assert queue.fail("w1") == "a"
assert queue.snapshot() == {"pending": ["b", "a"], "in_progress": {}}
assert not queue.enqueue("c")
assert queue.start("w2") == "b"
assert not queue.enqueue("c") # One pending item still fills capacity.
assert queue.start("w3") == "a"
assert queue.enqueue("c")The bound covers pending queue admissions. Items held by workers remain accepted work outside that count. A retry returns accepted work to the queue, even when this temporarily pushes pending length above max_pending. A failed item goes behind work already waiting, so repeated failures never jump it ahead of the queue.
Constraints
- Use FIFO order for pending work.
max_pendingis a nonnegative integer. Zero capacity rejects every enqueue. The constructor can assume these input preconditions.- Worker IDs are hashable identifiers with stable equality. Calls run sequentially in one thread; the exercise models worker state without launching worker threads.
- Treat
None/nullitems as outside the input specification. - Other falsey values, such as
0,False, and"", are valid items. Test ownership by worker membership, not item truthiness. - Capacity applies to pending items only on
enqueue; fail-retry bypasses the capacity check. - Don't allow one worker to hold two active items.