Complete a prerequisite, remove its outgoing obligations, and release a course only when none remain.
One finished prerequisite leaves course 3 waiting for the other
Read it this way:Course 3 requires courses 1 and 2. Its unmet count falls from two to one to zero, becoming ready only at zero. Is course three ready after only prerequisite one is emitted?
Sources and provenance
Full interview answer 22 min read
Determine whether every course can be completed while respecting all listed prerequisite requirements.
Problem
Input. A nonnegative course_count and pairs [course, prerequisite] using IDs from 0 through course_count - 1.
Output. Return True if some order completes every course; otherwise return False.
Rules. Each listed prerequisite must finish before its course can start.
All courses count, including isolated ones. Repeated pairs are allowed, and inputs remain unchanged.
Zero courses is feasible. A course requiring itself is not feasible.
Example. Four courses with pairs [[1, 0], [2, 0], [3, 1], [3, 2]] return True.
One possible order is [0, 1, 2, 3].
For two courses with pairs [[1, 0], [0, 1]], return False.
Picture
The label counts unmet incoming requirements.
Initially, only course 0 is ready:
Reference drawing
One finished prerequisite leaves course 3 waiting for the other
Derivation
Course 3 has two incoming requirements. Its unmet count changes 2 -> 1 -> 0 as the parents finish. Ready is not the same as already completed.
Read it this way:Course 3 requires courses 1 and 2. Its unmet count falls from two to one to zero, becoming ready only at zero. Is course three ready after only prerequisite one is emitted?
Completing 0 releases 1 and 2.
Completing 1 lowers course 3 from two requirements to one; it must still wait.
Completing 2 lowers that count to zero and releases 3.
Indegree counts incoming requirements not yet released.
After a course’s outgoing edges are processed, this equals incoming edges from unprocessed vertices.
A ready course has no remaining prerequisite.
Removing it deletes each outgoing requirement exactly once.
Duplicate pairs remain consistent parallel edges: count and decrement every copy.
Problem trace
Remove obligations as each ready course completes
Input and goalCheck whether all courses can be completed.
Build the dependency graphThe prerequisite diamond starts with indegrees [0,1,1,2]. Only course 0 is ready.
Directed prerequisite pairs ask whether every node can be placed after all requirements; failure to process every node means a directed cycle blocks the remainder.
Keep true
Every queued course has indegree zero in the remaining graph, completed counts dequeued courses, and each outgoing edge is removed exactly once when its prerequisite completes.
Reuse it
Repeatedly remove entities with no unmet requirements and decrement their dependents; the same mechanism orders builds, recipes, jobs, and dependency migrations, while leftovers certify a cycle.
The prerequisite diamond starts with indegrees [0,1,1,2]. Only course 0 is ready. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why is an empty ready queue alone insufficient to return true?
Problem trace
An empty ready queue proves success only if every course was emitted
Read it this way:An empty ready queue occurs after both a completed three-course chain and a blocked B-C cycle. Success additionally requires emitting every course. When the ready queue is empty but B and C still wait on each other, have all three courses completed?
Reference drawing
Problem trace
An empty ready queue proves success only if every course was emitted
Execution trace
Input and goalCheck whether all courses can be completed.
Build the dependency graphThe prerequisite diamond starts with indegrees [0,1,1,2]. Only course 0 is ready.
Directed prerequisite pairs ask whether every node can be placed after all requirements; failure to process every node means a directed cycle blocks the remainder.
Keep true
Every queued course has indegree zero in the remaining graph, completed counts dequeued courses, and each outgoing edge is removed exactly once when its prerequisite completes.
Reuse it
Repeatedly remove entities with no unmet requirements and decrement their dependents; the same mechanism orders builds, recipes, jobs, and dependency migrations, while leftovers certify a cycle.
The prerequisite diamond starts with indegrees [0,1,1,2]. Only course 0 is ready. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why is an empty ready queue alone insufficient to return true?
Read it this way:An empty ready queue occurs after both a completed three-course chain and a blocked B-C cycle. Success additionally requires emitting every course. When the ready queue is empty but B and C still wait on each other, have all three courses completed?
If all four courses are removed, all can finish.
If elimination stops early, every remaining vertex has an incoming requirement.
Following predecessors in that finite residual graph must repeat a vertex, proving a directed cycle.
Not every blocked vertex must itself lie on that cycle.
Code
from collections import dequedef can_finish(course_count: int, prerequisites: list[list[int]]) -> bool: graph = [[] for _ in range(course_count)] indegree = [0] * course_count for course, prerequisite in prerequisites: graph[prerequisite].append(course) indegree[course] += 1 ready = deque(course for course in range(course_count) if indegree[course] == 0) completed = 0 while ready: course = ready.popleft() completed += 1 for next_course in graph[course]: indegree[next_course] -= 1 if indegree[next_course] == 0: ready.append(next_course) return completed == course_count
Time and space are O(V+E), including the graph and queue.
Zero courses return True.
Eligibility is not shortest-distance priority, resource availability or a general scheduling guarantee.
Releasing course 3 after only course 1 would violate its second prerequisite.
Change to Parallel Rounds
New task: Group unit-duration courses under unlimited parallel capacity.
Picture and change: Freeze the ready count per round.
Newly released courses belong to the next round, not the current one.
def course_layers(course_count, prerequisites): graph = [[] for _ in range(course_count)] indegree = [0] * course_count for course, prerequisite in prerequisites: graph[prerequisite].append(course) indegree[course] += 1 ready = deque(course for course in range(course_count) if indegree[course] == 0) layers, completed = [], 0 while ready: layer = [] for _ in range(len(ready)): course = ready.popleft() layer.append(course) completed += 1 for dependent in graph[course]: indegree[dependent] -= 1 if indegree[dependent] == 0: ready.append(dependent) layers.append(layer) return layers if completed == course_count else None
Check: The diamond returns [[0],[1,2],[3]].
A cycle returns None; zero courses return [].
Three rounds are optimal only under the stated duration and capacity assumptions.
Use when: Dependency depth, not resource-constrained scheduling, is requested.
Change to Blocked-Node Output
New task: Return every residual blocked vertex.
Picture and change: Run elimination, then inspect positive remaining counts.
They identify blocked vertices, not an exact cycle witness.
def blocked_after_elimination(course_count, prerequisites): graph = [[] for _ in range(course_count)] indegree = [0] * course_count for course, prerequisite in prerequisites: graph[prerequisite].append(course) indegree[course] += 1 ready = deque(course for course in range(course_count) if indegree[course] == 0) while ready: course = ready.popleft() for dependent in graph[course]: indegree[dependent] -= 1 if indegree[dependent] == 0: ready.append(dependent) return [course for course in range(course_count) if indegree[course] > 0]
Check: With pairs [[1,0],[0,1],[2,1]] and four courses, return [0,1,2].
Course 3 completes; 0,1 form a cycle, while 2 is merely downstream.
Both adaptations remain O(V+E) in time and space.
Use when: Residual obstruction coverage is sufficient.
Transfer Check
Why cannot undirected union-find replace this directed-cycle test?
Answer
Connectivity forgets prerequisite direction.
The diamond has an undirected cycle but no directed cycle and remains feasible.
Representative equality therefore answers a different question.