Minimum Semesters with a Course Limit
Updated
Problem statement
There are n courses numbered from 0 to n - 1. Each row [course, previous] in prerequisites requires previous to be completed before course can start.
Every course takes exactly one semester. In each semester, you may take at most k distinct unfinished courses whose prerequisites were all completed in earlier semesters. A course and one of its prerequisites cannot be taken in the same semester.
Complete minSemesters. Return the smallest number of semesters needed to complete all courses exactly once, or -1 if that is impossible. Include every disconnected or isolated course. Repeated prerequisite rows repeat the same rule, while a self-prerequisite makes completion impossible.
Function
minSemesters(n: int, prerequisites: int[][], k: int) → intExamples
Example 1
n = 4prerequisites = [[2,0],[2,1],[3,2]]k = 2return = 3Take courses 0 and 1 together, then 2, then 3. Each prerequisite must finish in an earlier semester.
Example 2
n = 6prerequisites = [[3,2],[4,3],[5,4]]k = 2return = 4One optimal schedule is [0,2], [1,3], [4], [5]. Taking only the two smallest available IDs first would delay the chain and use five semesters.
Example 3
n = 2prerequisites = [[1,0],[0,1]]k = 2return = -1Neither course can start before the other finishes, so no complete schedule exists.
Constraints
1 <= n <= 12and1 <= k <= n.0 <= prerequisites.length <= 200.- Every prerequisite row has exactly two integers in
[0, n - 1]. Repeated rows, self-prerequisites and cycles are permitted.
More Snowflake Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Minimum Distance Matching in a ForestScale AI · PHONE SCREEN · ONSITE INTERVIEWScale AI interview questions
- Minimum Traveling Salesperson TourGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Shortest Round Trip Through All DeliveriesGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimum Semesters With Course CapacityAmazon · ONSITE INTERVIEW
- Shortest Path Visiting All NodesInMobi · ONSITE INTERVIEWInMobi interview questions
- Longest Anchored Song ChainIntuit · OAIntuit interview questions
- Cheapest Visiting Order with PrerequisitesIndeed · OAIndeed interview questions
- Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
- Select Transactions with Child-Pays-for-Parent PackagesCoinbase · PHONE SCREENCoinbase interview questions