Beyond Big O

Bounded worker scheduling

Bounded worker scheduling

Medium

Solution guide

Reveal solution guide — spoilers

Approach

  1. Represent active dispatches separately from the FIFO waiting queue and associate each active job with its current dispatch token.
  2. Process one explicit event at a time. Admit only within the worker and waiting limits, and accept completion only for the matching active token. Cancellation removes the current job state without accepting later stale completions.
  3. After an event frees a worker, dispatch waiting work in FIFO order and assign a fresh monotonic token. Report the exact post-event snapshot required by the contract.

Why it works

Separate active and waiting state makes both capacity bounds explicit. A dispatch token identifies an execution attempt rather than just a reusable job ID, so a late completion cannot finish a replacement. Processing explicit events models these ownership decisions deterministically; it does not test operating-system thread interleavings.

Complexity

Using maps and a linked FIFO gives expected O(1) state transitions plus the cost of each required snapshot. A simple array queue may use linear removal; total output size is a necessary lower bound on runtime and storage.

Edge cases to check

  • Zero waiting capacity still permits dispatch to an idle worker when the contract allows it.
  • Cancelling an active job can immediately release a worker for the oldest waiting job.
  • A reused ID needs a fresh dispatch token; repeated cancellation and stale completion must not mutate state.
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…