FastPrepMinimum Semesters With Course Capacity
Problems/Minimum Semesters With Course Capacity

Minimum Semesters With Course Capacity

Amazon logoAmazon● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon full-time onsite interview. The report talks about using the depth of what comes after a course as a quick rule in an interview, and notes it may not be best overall. Our version asks for the exact minimum and sets limits. It is about 92% the same as the reported question.

There are n courses labeled from 1 through n. Each pair [a, b] in prerequisites means course a must be finished before course b.

In one semester, you may take at most maxCourses courses. You can take a course only if all of its prerequisites were finished in earlier semesters. Return the minimum number of semesters needed to finish every course.

No chain of prerequisites ever loops back to a course it started from. When more than maxCourses courses are ready to take, choose the group that leads to the smallest possible number of semesters overall. Do not rely on a fixed rule for which ready courses to take first.

Function

minimumSemesters(n: int, prerequisites: int[][], maxCourses: int) → int

Examples

Example 1

n = 4prerequisites = [[1,3],[2,3],[3,4]]maxCourses = 2return = 3

Courses 1 and 2 have no prerequisites, so take both in the first semester. Then take course 3, then course 4, for three semesters in all.

Example 2

n = 4prerequisites = []maxCourses = 2return = 2

With no prerequisites, take any two courses in each semester, so all four courses take two semesters.

Constraints

  • 1 <= n <= 15
  • 0 <= prerequisites.length <= n * (n - 1) / 2
  • 1 <= maxCourses <= n
  • Every prerequisite pair contains two different course labels in [1, n].
  • No pair appears twice, and no chain of prerequisites loops back to a course it started from.
See Amazon hiring insights
Saved
class Solution {
    public int minimumSemesters(int n, int[][] prerequisites, int maxCourses) {
        // Write your code here.
    }
}
Checking account…