KatInterview

Course Plan Builder with Already-Passed Classes

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphstopological-sortgraphbfs

Last updated

Build a valid course order from prerequisite pairs while skipping passed courses, using a topological sort with a fixed tie-break order and cycle detection.

Course Plan Builder

A university portal needs to produce a study plan. Each course may require other courses to be finished first. A student has already passed some courses, and those never need to be taken again.

You are given:

  • courses: a list of unique course IDs (strings). Every ID that appears in prereq_pairs is also in this list.
  • prereq_pairs: a list of pairs [a, b] meaning course a must be completed before course b. Duplicate pairs may occur, and each copy counts separately.
  • passed_courses: a list of course IDs the student has already passed. It may contain IDs that are not in courses.

Return the list of courses the student still has to take, in an order where each course comes after all of its prerequisites. Passed courses never appear in the output. If the plan cannot be completed because of a circular dependency, return an empty list.

Required ordering

Several valid orders can exist, so the answer must be the one produced by this exact procedure:

  1. For every course, count how many pairs in prereq_pairs have it as the second element (b). Call this its requirement count.
  2. Create a first-in-first-out line. Scan courses in the given order and add a course to the line if it is in passed_courses or its requirement count is 0.
  3. Repeat until the line is empty:
    • Remove the course c at the front of the line.
    • If c is not passed, append it to the output schedule.
    • Mark c as handled. Passed courses count as handled from the very start.
    • Go through the pairs [c, d] in the order they appear in prereq_pairs. Reduce the requirement count of d by 1. If it reaches 0 and d is not handled, add d to the back of the line.
  4. If any course in courses was never handled, return []. Otherwise return the schedule.

A passed course is treated as available even if its own prerequisites are unmet, and the pairs leaving it are released when it reaches the front of the line. If every course is already passed, the result is [].

Constraints

  • 1 <= len(courses) <= 1000
  • Course IDs are non-empty strings, and the IDs in courses are unique.
  • Every ID in prereq_pairs appears in courses.
  • passed_courses may contain IDs that are not in courses.
  • The output contains only courses that are not passed.

Examples

Example 1

Input: plan_courses(['CS101', 'CS102', 'CS103'], [['CS101', 'CS102'], ['CS102', 'CS103']], ['CS101'])

Output: ['CS102', 'CS103']

Example 2

Input: plan_courses(['A', 'B'], [['A', 'B'], ['B', 'A']], [])

Output: []

Hints

Hint 1

Treat each pair [a, b] as a directed edge a -> b and keep a count of unmet incoming edges for every course.

Hint 2

A FIFO queue gives the required order. Seed it by scanning `courses` in order, and remember that passed courses are seeded too, even though they are never output.

Hint 3

Reaching the front of the queue is what releases a course's outgoing edges. After the loop, any listed course that was never handled means a cycle, so return an empty list.