Netflix interview question
Minimum number of semesters to finish all courses with prerequisites
- Company:
- Netflix
- Type:
- Coding
- Difficulty:
- Medium
- Reported:
- November 16, 2026
Problem
There are n courses labelled 1..n and a list of relations [prev, next], meaning course prev must be taken before course next. In one semester you can take any number of courses as long as all their prerequisites were completed in earlier semesters.
Return the minimum number of semesters needed to take all courses, or -1 if it is impossible (the prerequisites contain a cycle).
Solve it with BFS first; the follow-up asks for a DFS solution.
Example 1
Input: n = 3, relations = [[1,3],[2,3]]
Output: 2
Take 1 and 2 in semester one, then 3.
Example 2
Input: n = 3, relations = [[1,2],[2,3],[3,1]]
Output: -1
The prerequisites form a cycle.
Constraints
- Not stated; ask the interviewer.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern