MediumSchedulingPython 3
Multi-tenant Job Scheduler
Dispatch queued jobs by priority while preventing one tenant from monopolizing workers.
45m2 sample tests5 hidden tests
Implement FairJobScheduler, a priority scheduler with tenant fairness and cancellation.
Requirements
- Constructor receives
max_consecutive_per_org. enqueue(org, job_id, priority)adds a job. Lower priority number runs first.cancel(job_id)marks a pending job canceled and returns whether it existed.dispatch(count)returns up tocountjob IDs.- FIFO order breaks ties within the same priority.
- If another org has pending work, dispatch may not return more than
max_consecutive_per_orgjobs from the same org in a row. - Canceled jobs are skipped.
Example
python
1s = FairJobScheduler(2)
2s.enqueue("a", "a1", 1)
3s.enqueue("a", "a2", 1)
4s.enqueue("a", "a3", 1)
5s.enqueue("b", "b1", 5)
6assert s.dispatch(4) == ["a1", "a2", "b1", "a3"]Constraints
- Keep state in memory.
- Optimize for correctness and clear invariants before heap efficiency.
Editor