HardSchedulingPython 3
Dependency Scheduler With Retries
Run a dependency graph deterministically, retry transient failures, and reject invalid graphs.
45m1 sample tests1 hidden tests
Dependency Scheduler With Retries
Implement schedule(tasks, run, max_attempts=2).
Requirements
tasksis a list of dictionaries withidanddeps.- A task can run when every dependency has completed.
- Pick ready tasks in lexicographic order for deterministic output.
- Call
run(task_id)for each attempt. - If
runraises, retry the task untilmax_attemptsis exhausted. - Return task IDs in successful completion order.
- Raise
ValueErrorfor cycles or missing dependencies. - Raise
RuntimeErrorwhen a task fails all attempts.
Example
python
1tasks = [
2 {"id": "extract", "deps": []},
3 {"id": "embed", "deps": ["extract"]},
4 {"id": "index", "deps": ["embed"]},
5]
6
7assert schedule(tasks, lambda task_id: None) == ["extract", "embed", "index"]Constraints
- Single-threaded base version.
- Use standard-library Python only.
Editor
1 2
Sample Tests
runs dependencies before dependents
from solution import schedule
tasks = [
{"id": "index", "deps": ["embed"]},
{"id": "extract", "deps": []},
{"id": "embed", "deps": ["extract"]},
]
seen = []
assert schedule(tasks, lambda task_id: seen.append(task_id)) == ["extract", "embed", "index"]
assert seen == ["extract", "embed", "index"]Results
Run sample tests or submit all tests.