Skip to main content
  1. LeetCode/

LeetCode 210: Course Schedule II

·2 mins· ·
LeetCode Medium Graph Topological-Sort
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-08 Source: Day 1 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Learning Note Extract
#

LC 210 — Course Schedule II (Topological Sort / Return Order)
#

  • Pattern: Same as LC 207 but return the actual ordering
  • Key insight: The order nodes are popped from the queue IS the topological order
  • Difference from LC 207: Append each popped node to result list. If len(result) == numCourses → valid order exists.

Organized Notes
#

This is the order-returning version of LC 207. The graph and indegree construction are identical: an edge goes from prerequisite to course. The difference is that every popped node is appended to order, and the result is valid only if the order covers every course. If a cycle remains, some indegrees never drop to zero, so returning the partial order would be wrong.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from collections import deque
from typing import List

class Solution:
    def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
        graph = [[] for _ in range(numCourses)]
        indeg = [0] * numCourses
        for course, pre in prerequisites:
            graph[pre].append(course)
            indeg[course] += 1

        q = deque(i for i in range(numCourses) if indeg[i] == 0)
        order = []
        while q:
            node = q.popleft()
            order.append(node)
            for nei in graph[node]:
                indeg[nei] -= 1
                if indeg[nei] == 0:
                    q.append(nei)

        return order if len(order) == numCourses else []

Complexity
#

Time O(V+E), Space O(V+E).

Mistakes To Watch
#

  • Returning partial order when a cycle remains.
  • Confusing prerequisite edge direction.

Final Interview Explanation
#

I would use the same Kahn topo process as Course Schedule, but append each popped course to an order list. The order is valid only if it contains all courses; otherwise a cycle blocked some courses, and the correct return value is an empty list.

Related

LeetCode 207: Course Schedule
·2 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Course Schedule, rebuilt from the original learning note
LeetCode 2192: All Ancestors of a Node in a DAG
·3 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for All Ancestors of a Node in a DAG, rebuilt from the original learning note
LeetCode 1971: Find if Path Exists in Graph
·2 mins
LeetCode Easy Graph Union-Find
LeetCode note for Find if Path Exists in Graph, rebuilt from the original learning note
LeetCode 1497: Check If Array Pairs Are Divisible by k (Remainder Pairing, Modulo)
·2 mins
LeetCode Daily Medium Array Hash-Map Complement Counting Math Modulo
Remainder pairing with a hash map; handle 0 and k/2 remainders carefully.
LeetCode 1695: Maximum Erasure Value (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Hash-Set
Sliding window with a set and running sum; two pointers to keep the subarray unique.
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.