MediumSchedulingPython 3
Priority Deadline Scheduler
Schedule due tasks by priority while skipping not-yet-ready and expired work deterministically.
40m1 sample tests1 hidden tests
Build a scheduler that releases tasks only when they are due and skips expired tasks.
Requirements
- Define
DeadlineScheduler(now). add(task_id, priority, run_at=0, ttl=None)stores a task.pop()returns the due, non-expired task with highest priority.- Higher numeric
prioritywins. - Ties use insertion order.
- A task is due when
run_at <= now(). - A task with
ttlexpires whennow() >= run_at + ttl. - Expired tasks are treated as missing.
Example
python
1clock = {"t": 0}
2scheduler = DeadlineScheduler(now=lambda: clock["t"])
3scheduler.add("later", priority=10, run_at=5)
4scheduler.add("now", priority=1, run_at=0)
5assert scheduler.pop() == "now"
6clock["t"] = 5
7assert scheduler.pop() == "later"Constraints
- Use deterministic injected time.
- Do not return tasks before
run_at. - Do not let expired high-priority tasks block lower-priority due tasks.
Editor
Results
Run sample tests or submit all tests.