WorkspaceProblem lab
ALGORITHM VISUALIZER

Course Schedule

Opening the interactive workspace…

Understanding Course Schedule

Return whether all n courses can be completed. Each pair [course, prerequisite] means the prerequisite must be completed first. The walkthrough removes courses with no remaining prerequisites.

How it works

A cycle leaves some courses permanently blocked, even after every available course is removed.

Count incoming edges, queue zero-indegree courses, and remove their outgoing edges. All courses are feasible exactly when n courses are processed.

Try an example

Input: n = 4, prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]]

Output: true

One possible order is 0, 1, 2, 3.

Constraints

  • 0–16 courses and at most 48 prerequisite pairs.
  • Each course ID must be between 0 and n − 1.
  • A self-dependency makes completion impossible.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity