← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Practice/Netflix/Parallel Courses
CodingMust
You are given an integer n representing courses labeled from 1 to n, and an array relations where relations[i] = [prevCourse, nextCourse] indicates that prevCourse must be taken before nextCourse.
In one semester, you can take any number of courses as long as you have completed all prerequisites in previous semesters.
Return the minimum number of semesters needed to complete all courses. If there is a circular dependency (making it impossible to complete all courses), return -1.
This is a classic topological sort problem that can be solved using Kahn's algorithm (BFS-based) or DFS-based cycle detection.
` n = 3 relations = [[1,3],[2,3]]
`
` n = 3 relations = [[1,2],[2,3],[3,1]]
`