Check whether a prerequisite graph allows all courses to finish

Quick Overview

Check whether every course can be completed under directed prerequisites. Include disconnected components and repeated dependencies, and explain why an unprocessed cycle makes the overall schedule impossible.

Check whether a prerequisite graph allows all courses to finish

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You must finish `n` courses labeled `0` through `n - 1`. Each pair `[a, b]` means course `b` must be completed before course `a`. Return whether a completion order exists for every course. ### Function Signature ```python def courses_are_possible(n: int, prerequisites: list[list[int]]) -> bool: ``` ### Rules - Every dependency is mandatory, including those in disconnected parts of the graph. - Repeated pairs have the same effect as one occurrence. - A course may appear as its own prerequisite. Such an input is impossible to complete. - Return `True` for zero courses. ### Constraints These limits are exercise assumptions for the referenced prerequisite problem. - `0 <= n <= 100000`. - `0 <= len(prerequisites) <= 200000`. - Each pair contains exactly two integers from `0` through `n - 1`. - When `n == 0`, the prerequisite list is empty. ### Examples ```text Input: n = 3, prerequisites = [[2, 1], [1, 0]] Output: True ``` Courses can be completed in the order zero, one, two. ```text Input: n = 4, prerequisites = [[1, 0], [2, 3], [3, 2]] Output: False ``` Courses zero and one can be completed, but courses two and three form a cycle.

Overview: Check whether every course can be completed under directed prerequisites. Include disconnected components and repeated dependencies, and explain why an unprocessed cycle makes the overall schedule impossible.

ByteDance logo
ByteDance
Nov 26, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

You must finish n courses labeled 0 through n - 1. Each pair [a, b] means course b must be completed before course a. Return whether a completion order exists for every course.

Function Signature

def courses_are_possible(n: int, prerequisites: list[list[int]]) -> bool:

Rules

  • Every dependency is mandatory, including those in disconnected parts of the graph.
  • Repeated pairs have the same effect as one occurrence.
  • A course may appear as its own prerequisite. Such an input is impossible to complete.
  • Return True for zero courses.

Constraints

These limits are exercise assumptions for the referenced prerequisite problem.

  • 0 <= n <= 100000 .
  • 0 <= len(prerequisites) <= 200000 .
  • Each pair contains exactly two integers from 0 through n - 1 .
  • When n == 0 , the prerequisite list is empty.

Examples

Input: n = 3, prerequisites = [[2, 1], [1, 0]]
Output: True

Courses can be completed in the order zero, one, two.

Input: n = 4, prerequisites = [[1, 0], [2, 3], [3, 2]]
Output: False

Courses zero and one can be completed, but courses two and three form a cycle.