Project

Task Scheduler

Build a production task scheduler using a priority queue for ordering and topological sort for dependency resolution. This is how Celery, Airflow, and OS process schedulers work at their core.

Dependency resolution — animated

Tasks A, B, C — C depends on A and Bstep 1/4

// Array state

A(1)
heap
B(2)
heap
C(wait)
wait
_
[3]
_
[4]
→ Tasks: A(pri=1,no deps), B(pri=2,no deps), C(pri=3,deps=[A,B]). A and B added to heap.

// Pseudocode

1// Task Scheduler — priority queue core
2class TaskScheduler:
3 readyQueue = MinHeap(by=priority) // tasks ready to run
4 waitingTasks = HashMap<taskId, Task>() // blocked on deps
5 dependencyCount = HashMap<taskId, int>() // in-degree
6 depGraph = HashMap<taskId, List<taskId>>() // edges
7 
8 function addTask(task):
9 dependencyCount[task.id] = len(task.deps)
10 if dependencyCount[task.id] == 0:
11 readyQueue.push(task) // no deps, ready now
12 else:
13 waitingTasks[task.id] = task
14 
15 function runNext():
16 task = readyQueue.pop() // highest-priority ready task
17 execute(task)
18 for child in depGraph[task.id]:
19 dependencyCount[child] -= 1
20 if dependencyCount[child] == 0:
21 readyQueue.push(waitingTasks[child]) // unblock

Design layers

1 / 4
1

Priority Queue for scheduling

A min-heap lets you always pull the highest-priority task in O(log n). EDF (Earliest Deadline First) sets priority = deadline. SJF (Shortest Job First) sets priority = estimated duration.

1 of 4

Core pseudocode

task-scheduler.pseudo
1// Task Scheduler — priority queue core
2class TaskScheduler:
3 readyQueue = MinHeap(by=priority) // tasks ready to run
4 waitingTasks = HashMap<taskId, Task>() // blocked on deps
5 dependencyCount = HashMap<taskId, int>() // in-degree
6 depGraph = HashMap<taskId, List<taskId>>() // edges
7 
8 function addTask(task):
9 dependencyCount[task.id] = len(task.deps)
10 if dependencyCount[task.id] == 0:
11 readyQueue.push(task) // no deps, ready now
12 else:
13 waitingTasks[task.id] = task
14 
15 function runNext():
16 task = readyQueue.pop() // highest-priority ready task
17 execute(task)
18 for child in depGraph[task.id]:
19 dependencyCount[child] -= 1
20 if dependencyCount[child] == 0:
21 readyQueue.push(waitingTasks[child]) // unblock

Complexity

addTask — heap insert for ready tasksO(log n)

Logarithmic time

runNext — heap pop + dependency updatesO(log n)

Logarithmic time

Total scheduling — topological sort over all tasks/depsO(V + E)

O(V + E) time

Space — heap + waiting map + dep graphO(n)

Linear time

🔍Real-world schedulers
Apache Airflow uses DAG (Directed Acyclic Graph) with topological ordering for pipeline dependencies. Linux CFS (Completely Fair Scheduler) uses a red-black tree sorted by virtual runtime for process scheduling. Kubernetes uses a priority queue for pod scheduling across nodes.
⚠️Watch for circular dependencies
If all remaining tasks have in-degree greater than zero after scheduling, there is a cycle. Always validate the task graph for cycles before starting execution — throw an error early.

🎯 Quick Check

What scheduling policy does a min-heap sorted by deadline implement?

Social Network
end of projects