MediumSchedulingPython 3
Bounded Worker Queue
Model a bounded FIFO work queue with explicit in-progress ownership and retry behavior. Items are non-null.
35m3 sample tests7 hidden tests
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
python
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:
python
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 controls admission of new pending work. It does not bound all accepted work across workers, and a retry is not a new admission. A failed item goes behind work already waiting, so repeatedly failing one item cannot jump it ahead of the queue.
Constraints
- Use FIFO order for pending work.
max_pendingis a nonnegative integer. Zero capacity rejects every enqueue. Inputs meet these preconditions; constructor validation is not required.- Worker IDs are hashable identifiers with stable equality. Calls are single-threaded; the exercise does not start actual worker threads.
- Treat
None/nullitems as outside the input contract. - 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.
Editor