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

Course Schedule II (Topo Sort Order)

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Course Schedule II (Topo Sort Order)** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Kahn's BFS: queue nodes with in-degree 0 **Edge Cases to Consider:** - (1) No valid ordering (cycle) - (2) No prerequisites (any order) - (3) Linear dependency chain

Examples

Example 1
Input: 2, [[1,0]]
Output: [0,1]
Explanation: Simple
Example 2
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: Multiple valid
Example 3
Input: 1, []
Output: [0]
Explanation: Single course

Constraints

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

Watch Out For Edge Cases

  • ▪No valid ordering (cycle)
  • ▪No prerequisites (any order)
  • ▪Linear dependency chain
Frequently Asked At
AmazonGoogleMicrosoftAdobeUber