I knew this was a bitmask DP problem pretty quickly, which felt good for about 30 seconds until I started fumbling the state transitions.
Model the courses and prerequisites as a directed graph and use topological sorting to process courses semester by semester. In each semester, take up to k courses that have no remaining prerequisites, and if at any point no courses can be taken but courses remain, return -1. Count the number of semesters needed.
Pro tip: Clarify edge cases upfront, such as when k is larger than the number of available courses or when there are multiple valid orderings; this shows attention to detail and can guide the interviewer's expectations.
Represent courses as nodes and prerequisites as directed edges. Compute the in-degree (number of prerequisites) for each course.
Collect all courses with in-degree 0 into a queue or list, as these can be taken in the first semester.
While there are available courses, take up to k courses per semester. For each taken course, reduce the in-degree of its dependents; if any dependent's in-degree becomes 0, add it to the next semester's available list.
If at any semester no courses can be taken but courses remain, return -1 (cycle detected). Otherwise, increment the semester count until all courses are taken.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.