Mid
Detailed answer
Graphs
DSA & Coding Interviews
Explain Course Schedule (detect cycle in a directed graph).
Short answer: Model prerequisites as a directed graph. Detect a cycle with DFS colors (white/gray/black) or Kahn’s algorithm (BFS indegrees). If a cycle exists, you cannot finish all courses.
Complexity
Time O(V + E), Space O(V + E).
Common follow-ups
- Course Schedule II — return a valid order (topological sort)
- Parallel semester counting
Say “topological sort / cycle detection” early — that is the pattern name they want.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png