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.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details