Data Structures & Algorithms Advanced Graphs
💡
Exercise 118

Course Schedule II 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Return the ordering of courses you should take. If impossible (cycle), return empty list. This is topological sort applied to prerequisites.

Use Kahn's algorithm. If the result has fewer than numCourses nodes, there's a cycle.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Return a valid course ordering. Print [] if impossible.
Kahn's: compute in-degrees, BFS from in-degree 0 nodes. If len(order) < numCourses, cycle exists.
🧪 Test Cases
Input
findOrder(4, [[1,0],[2,0],[3,1],[3,2]])
Expected
[0, 1, 2, 3]
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.