#210

Course Schedule II

MediumTopological SortHas vizDFSBFSGraphTopological Sort

Problem

Course Schedule II

You must solve the interview problem titled "Course Schedule II".

Difficulty: Medium. Topics: DFS, BFS, Graph, Topological Sort. Primary pattern: Topological Sort.

Read the title as the specification: implement the exact behavior it names, return the correct output type, and aim for an interview-optimal Topological Sort solution. Use the examples to lock input/output format, then confirm edge cases (empty, single element, duplicates, extremes).

Example 1

Input: sample input for Course Schedule II

Output: sample output for Course Schedule II

Apply a Topological Sort approach and match the required return type.

Example 2

Input: edge-case input for Course Schedule II

Output: edge-case output for Course Schedule II

Verify empty/single/boundary behavior.

Constraints

  • Interview-scale limits for a Medium Topological Sort problem.
  • Handle empty and single-element inputs when they are valid.
  • Prefer the optimal asymptotic complexity expected in interviews.
Loading visualization…

Explanation

Course Schedule II is a Medium interview problem tagged [DFS, BFS, Graph, Topological Sort]. The winning approach is typically **Topological Sort**.

Intuition

Dependency edges define a partial order you must respect.

Approach

Kahn (indegree queue) or DFS finish-order on a DAG; detect cycles if not all nodes ordered.

  1. Build indegrees.
  2. Queue zeros.
  3. Reduce neighbors.
  4. Fail if processed < n.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateTopological Sort

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) or O(n)

Space: O(n)

Before coding, restate the problem, name the pattern (Topological Sort), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #210 Course Schedule II as understood when the pattern clicks.