Schedule prerequisite classes with retakes
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Quick Answer: This question evaluates competency in graph algorithms, dependency resolution, stateful scheduling, and robust error handling within the Coding & Algorithms domain.
Constraints
- Only the prerequisite subgraph reachable from `roots` matters; unrelated courses should be ignored.
- Up to 100000 reachable courses and 200000 reachable prerequisite edges.
- 0 <= max_retries <= 10.
- Course IDs are integers; `roots` may contain duplicates or be empty.
Examples
Input: ({1: [], 2: [1], 3: [1], 4: [2, 3], 5: [2]}, [4, 5, 4], {1: [True], 2: [False, True], 3: [True], 4: [True], 5: [True]}, 2)
Expected Output: ('success', [1, 2, 3, 2, 4, 5])
Explanation: The reachable courses are 1, 2, 3, 4, and 5. Course 1 passes first. Course 2 fails once and goes to the back of the queue, so 3 is attempted next and passes. Then 2 passes on retry, unlocking both 4 and 5. Both target roots are eventually passed.
Input: ({1: [2], 2: [3], 3: [1]}, [1], {}, 2)
Expected Output: ('error', 'cycle detected')
Explanation: The reachable subgraph contains the cycle 1 -> 2 -> 3 -> 1, so no valid prerequisite order exists.
Input: ({1: [2], 2: [4], 3: []}, [1], {}, 1)
Expected Output: ('error', 'missing prerequisite: 4')
Explanation: Course 4 is referenced as a prerequisite of reachable course 2 but does not exist in the course dictionary.
Input: ({1: [], 2: [1]}, [2], {1: [False, False, False]}, 2)
Expected Output: ('error', 'max retries exceeded: 1')
Explanation: Course 1 is the only available course. It fails three times. With `max_retries = 2`, the third failure exceeds the limit.
Input: ({7: []}, [7], {7: [False, False, True]}, 2)
Expected Output: ('success', [7, 7, 7])
Explanation: Exactly two failed attempts are allowed here. Course 7 fails twice, then succeeds on the third attempt, so the schedule still completes successfully.
Input: ({1: [], 2: [1]}, [], {}, 3)
Expected Output: ('success', [])
Explanation: There are no target courses, so the student already satisfies all goals without attempting anything.
Hints
- The input edges point from course -> prerequisite, but scheduling becomes much easier if you also build the reverse relation prerequisite -> dependents and track how many prerequisites each course still needs.
- Detect cycles on the reachable subgraph before simulating retries. Then use a queue of currently available courses, re-enqueueing failed courses and unlocking dependents only after a prerequisite passes.