Course Schedule
Solve Course Schedule by recognizing the Graphs pattern and turning the prompt into a small invariant before coding.
Frame the problem
- Implement can_finish with the exact signature used by the interactive runner.
- Use the visible tests to confirm the input and output shape before reading the final solution.
- Treat challenge tests as edge-case pressure: empty inputs, repeated values, boundary shapes, or impossible states.
- State the invariant before code, then dry-run one passing case and one failing-looking case.
1. Reveal example inputs and outputs
Example 1
Input:
can_finish(2, [
[
1,
0
]
]) Output:
true 2. Brute force first
What direct brute force would be correct for a tiny input? Name the exact repeated work that the target pattern removes.
3. Reveal the insight ladder
- Map the prompt to the Graphs pattern instead of starting from syntax.
- A course with indegree zero can be taken first.
- If no node in the cycle ever reaches indegree zero, not all courses can be completed.
- Only reveal the final code after you can explain why each state update is safe.
4. Dry run before code
- course-schedule-possible: input [2,[[1,0]]] should produce true. Hint to check your state: A course with indegree zero can be taken first.
- course-schedule-cycle: input [2,[[1,0],[0,1]]] should produce false. Hint to check your state: If no node in the cycle ever reaches indegree zero, not all courses can be completed.
5. Reveal final Python solution
def can_finish(num_courses: int, prerequisites: list[list[int]]) -> bool:
graph = [[] for _ in range(num_courses)]
indegree = [0] * num_courses
for course, prereq in prerequisites:
graph[prereq].append(course)
indegree[course] += 1
queue = deque(course for course, degree in enumerate(indegree) if degree == 0)
completed = 0
while queue:
course = queue.popleft()
completed += 1
for next_course in graph[course]:
indegree[next_course] -= 1
if indegree[next_course] == 0:
queue.append(next_course)
return completed == num_courses Complexity: Derive the exact bounds from can_finish: count how often each input item is visited and the maximum size of the main state structure.
Interview narration
- I will first describe the invariant in plain language.
- Then I will explain what data structure carries that invariant across the traversal, loop, recursion, or DP transition.
- Finally I will walk one edge case before writing the optimized version.
Common traps
- Solving only the visible example instead of the invariant.
- Forgetting empty input, singleton input, duplicate values, or impossible-state cases.
- Revealing the solution before doing a dry run from the starter signature.
Follow-up drills
1. How do you turn this into a timed interview answer?
Start with the invariant, give the brute force in one sentence, name the optimized state, code the core loop or recursion, and run one visible test aloud before mentioning complexity.
2. How do you scale the same pattern to a larger input?
Track which state grows with the input: hash maps and sets grow with distinct values, queues grow with frontier width, recursion grows with depth, heaps grow with active candidates, and DP tables grow with state count.
3. What should you practice from blank tomorrow?
Rewrite can_finish without looking at the solution, then compare only the invariant and state updates before checking syntax.
Interactive runner
Write the required Python function. Your code runs locally in this browser. Hints reveal one failing case at a time.