PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates graph theory and scheduling competencies, including dependency resolution, cycle detection in directed graphs, and capacity-constrained task scheduling based on prerequisites.

  • medium
  • Netflix
  • Coding & Algorithms
  • Software Engineer

Compute minimum semesters to finish courses

Company: Netflix

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given `n` courses labeled `1..n` and a list of prerequisite relations `prereq`, where each element is a pair `[a, b]` meaning you must complete course `a` before taking course `b`. ### Part A Return the **minimum number of semesters** needed to complete all courses if you may take **any number of courses per semester**, as long as prerequisites are satisfied. - If it is impossible to finish all courses (because prerequisites contain a cycle), return `-1`. **Input** - Integer `n` - List `prereq: List[List[int]]` **Output** - Integer minimum semesters, or `-1` **Constraints (reasonable interview constraints)** - `1 <= n <= 10^5` - `0 <= prereq.length <= 2*10^5` - Course labels are in `1..n` ### Part B (follow-up) Now you may take **at most `k` courses per semester**. Return the minimum number of semesters needed to complete all courses under this constraint, or `-1` if impossible. **Additional input** - Integer `k` with `1 <= k <= n` **Notes** - You may choose which available (prerequisites-satisfied) courses to take each semester.

Quick Answer: This question evaluates graph theory and scheduling competencies, including dependency resolution, cycle detection in directed graphs, and capacity-constrained task scheduling based on prerequisites.

Part 1: Minimum Semesters with Unlimited Courses per Semester

You are given n courses labeled 1 through n and a list of prerequisite relations prereq. Each relation [a, b] means course a must be completed before course b. In one semester, you may take any number of courses, but every course taken in that semester must have all of its prerequisites completed in earlier semesters. Return the minimum number of semesters needed to finish all courses. If the prerequisite graph contains a cycle, return -1.

Constraints

  • 1 <= n <= 100000
  • 0 <= len(prereq) <= 200000
  • 1 <= a, b <= n
  • All prerequisite pairs are distinct.

Examples

Input: (3, [[1, 3], [2, 3]])

Expected Output:

Explanation: Courses 1 and 2 can be taken in semester 1, then course 3 in semester 2.

Input: (4, [[1, 2], [2, 3], [3, 4]])

Expected Output:

Explanation: This is a chain, so only one new course becomes available each semester.

Input: (3, [[1, 2], [2, 3], [3, 1]])

Expected Output:

Explanation: The courses form a cycle, so no valid completion order exists.

Input: (1, [])

Expected Output:

Explanation: Edge case: a single course with no prerequisites takes one semester.

Input: (4, [])

Expected Output:

Explanation: With no prerequisites, all courses can be taken in the first semester.

Hints

  1. Courses with zero remaining prerequisites can all be taken in the current semester.
  2. If you process the graph level by level with topological sorting, each level corresponds to one semester.

Part 2: Minimum Semesters with at Most k Courses per Semester

You are given n courses labeled 1 through n, a list of prerequisite relations prereq, and an integer k. Each relation [a, b] means course a must be completed before course b. In one semester, you may take at most k courses, and every course taken in that semester must have all prerequisites completed in earlier semesters. Return the minimum number of semesters needed to finish all courses, or -1 if it is impossible. For this exact version, assume n is small enough for a bitmask-based search.

Constraints

  • 1 <= n <= 15
  • 0 <= len(prereq) <= n * (n - 1) / 2
  • 1 <= k <= n
  • 1 <= a, b <= n
  • All prerequisite pairs are distinct.

Examples

Input: (4, [[1, 4], [2, 4]], 2)

Expected Output:

Explanation: Take courses 1 and 2 first to unlock 4, then take 3 and 4 in the second semester.

Input: (4, [[1, 2], [1, 3], [2, 4], [3, 4]], 2)

Expected Output:

Explanation: Take 1, then 2 and 3, then 4.

Input: (5, [], 2)

Expected Output:

Explanation: With no prerequisites, the answer is ceil(5 / 2) = 3.

Input: (3, [[1, 2], [2, 3], [3, 1]], 2)

Expected Output:

Explanation: A cycle makes it impossible to finish all courses.

Input: (1, [], 1)

Expected Output:

Explanation: Edge case: one course and capacity 1.

Hints

  1. Represent the set of completed courses as a bitmask.
  2. From each state, find all currently available courses. If there are more than k, try every subset of size k and use BFS or DP to find the fewest semesters.
Last updated: May 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Minimum Task Completion Time - Netflix (medium)
  • Solve String Arrays and Row Deduplication - Netflix (medium)
  • Implement Cache, Undo, and DFS - Netflix
  • Implement Streaming Word Counter - Netflix (medium)
  • Implement TTL Cache and Tree Balance Reporting - Netflix (medium)