DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Graph

Course Schedule (Cycle Detection)

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Course Schedule (Cycle Detection)** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Topological sort via DFS (white/gray/black) or Kahn's BFS **Edge Cases to Consider:** - (1) No prerequisites - (2) Self-loop - (3) Multiple independent cycles

Examples

Example 1
Input: 2, [[1,0]]
Output: true
Explanation: Simple dependency
Example 2
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: Cycle
Example 3
Input: 1, []
Output: true
Explanation: Single course, no prereqs

Constraints

  • ▪2 <= vertices <= 1000
  • ▪0 <= edges <= 5000
  • ▪Avoid infinite cycles during depth traversal

Watch Out For Edge Cases

  • ▪No prerequisites
  • ▪Self-loop
  • ▪Multiple independent cycles
Frequently Asked At
AmazonGoogleMicrosoftMetaAdobeFlipkart