2Brute2Best
🔥0

Course Schedule II

Medium·LeetCode #210 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

There are numCourses courses, labelled 0 to numCourses − 1. Each pair [a, b] in prerequisites means you must finish course b before you can take course a.

Return an order in which you can take all the courses. If several orders work, return any one of them. If no order works (the prerequisites form a loop), return an empty list.

Input

numCourses, and prerequisites, a list of pairs [a, b].

Output

A list with every course once, in an order that respects every pair. Or an empty list when no such order exists.

Example 1
Input: numCourses = 2, prerequisites = [[1, 0]]
Output: [0, 1]

To take course 1 you must finish course 0 first, so 0 comes before 1.

Example 2
Input: numCourses = 4, prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]]
Output: [0, 1, 2, 3] (or [0, 2, 1, 3])

Course 0 needs nothing. Courses 1 and 2 both need only 0. Course 3 needs both 1 and 2, so it comes last.

Example 3
Input: numCourses = 2, prerequisites = [[0, 1], [1, 0]]
Output: []

Course 0 needs 1 and course 1 needs 0. Neither can be taken first, so there is no valid order.

Constraints
  • • 1 ≤ numCourses ≤ 2000
  • • 0 ≤ prerequisites.length ≤ numCourses · (numCourses − 1)
  • • prerequisites[i].length == 2
  • • 0 ≤ a, b < numCourses
  • • a ≠ b
  • • All the pairs [a, b] are distinct.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“return an order”
→ We need the sequence itself, not just yes or no, so the queue order is the answer.
“[a, b] means b before a”
→ A directed arrow b → a: the prerequisite points to the course that needs it.
“return an empty list if impossible”
→ A cycle means some courses are never taken, so the order is short.
03

Approach Complexity Ladder

Brute Force — rescan for a free courseO(N · (N + P))O(N)

Each round, take the first course whose prerequisites are all taken; stop when none is free

Optimal — Kahn's queue, empty answer if shortO(N + P)O(N + P)

Count prerequisites once; queue the courses with none left; return [] if not all were taken

Brute Force Intuition

Keep track of the taken courses. In each round, rebuild the list of blocked courses from all the pairs, take the first course that is not blocked, and repeat. If a round finds no free course while some are left, they wait on each other, so return an empty list.

Recognition rule for this sub-pattern:

Queue all in-degree-0 nodes; removing one lowers its neighbours' in-degree.
Remember this · Topological Sort
Trigger
Prerequisites, dependencies, build order, "can all courses be finished?", ordering letters from sorted words.
Think
Is this a directed graph where some things must happen before others?
Key question
Which nodes currently have in-degree 0?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.