MediumSchedulingPython 3

Bounded Worker Queue

Model a bounded FIFO work queue with explicit in-progress ownership and retry behavior.

35m1 sample tests1 hidden tests

Implement a deterministic FIFO queue that tracks pending and in-progress work.

Requirements

  • Define WorkQueue(max_pending).
  • enqueue(item) appends pending work and returns True; if pending capacity is full, return False.
  • start(worker_id) assigns the next pending item to an idle worker and returns it.
  • If a worker already has in-progress work, start(worker_id) returns None.
  • complete(worker_id) removes and returns the worker's in-progress item.
  • fail(worker_id, retry=True) removes the worker's item and optionally requeues it at the back.
  • snapshot() returns {"pending": [...], "in_progress": {...}}.

Example

python
1queue = WorkQueue(max_pending=2) 2assert queue.enqueue("a") 3assert queue.enqueue("b") 4assert not queue.enqueue("c") 5assert queue.start("w1") == "a" 6assert queue.enqueue("c") 7assert queue.complete("w1") == "a"

Constraints

  • Use FIFO order for pending work.
  • Capacity applies to pending items only.
  • Do not allow one worker to hold two active items.

Editor
Results
Run sample tests or submit all tests.