Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

numCourses =
prerequisites =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Return a topological ordering if it exists.

Approach

Kahn’s algorithm (BFS topo sort):

  • Build graph b -> a.
  • Queue all indegree-0 nodes.
  • Pop, append to order, reduce indegrees. If order length != numCourses, a cycle exists.
Code
Loading...
Complexity
Time: O(V+E)
Space: O(V+E)
Solution
Updated: 2026-02-23

Idea

Return a topological ordering if it exists.

Approach

Kahn’s algorithm (BFS topo sort):

  • Build graph b -> a.
  • Queue all indegree-0 nodes.
  • Pop, append to order, reduce indegrees. If order length != numCourses, a cycle exists.
Code
Loading...
Complexity
Time: O(V+E)
Space: O(V+E)