Course Schedule
Python data engineering interview problem. Difficulty: advanced. Pattern: Graphs. About 18 minutes. Part of the Pro drill bank.
Whether all courses can be finished given prerequisites. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A course scheduler must take prerequisites before the courses that depend on them. A cycle means no course in that cycle can be completed first. Return whether all courses can be finished. Each pair [course, prereq] means the prerequisite points to the course and must come first. Write can_finish(num_courses: int, prerequisites: list) -> bool. With 2 courses and [[1, 0]], course 0 can be completed before 1, so the answer is true. The starter file already has the function name and harness print lines. Keep them and fill only the function body.
Requirements
- Build dependency relationships in the stated direction.
- Detect a cycle or an impossible dependency count.
- Return a boolean indicating whether all courses are finishable.
Constraints
- Courses are numbered from 0 to num_courses - 1.
- A prerequisite pair is [course, prereq], and the return type is bool.
Examples
Input: can_finish(2, [[1, 0]]) Output: True Linear order 0 then 1.
Topics: lakebench, python, topo, cycle.
More Python interview questions · All interview problems · Learn data engineering
Course Schedule
Interview-style drill: Whether all courses can be finished given prerequisites.
Implement `can_finish(num_courses: int, prerequisites: list) -> bool`. prerequisites entries are `[course, prereq]` meaning prereq → course. Example: `2, [[1, 0]]` → `True`; `2, [[1, 0], [0, 1]]` → `False`. Keep the harness.