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.

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.

Constraints

  • `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: (3, [[2, 1], [1, 0]])

Expected Output: True

Explanation: A simple chain has a valid completion order.

Input: (4, [[1, 0], [2, 3], [3, 2]])

Expected Output: False

Explanation: A detached cycle makes the full schedule impossible.

Hints

  1. Cycles must be detected in every disconnected part of the graph.
  2. Compare a prerequisite that is currently being explored with one whose entire subtree is already complete.

Check whether a prerequisite graph allows all courses to finish

mediumByteDanceSoftware Engineer

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.

Examples

Example 1
Input
(3, [[2, 1], [1, 0]])
Output
True
Notes
A simple chain has a valid completion order.
Example 2
Input
(4, [[1, 0], [2, 3], [3, 2]])
Output
False
Notes
A detached cycle makes the full schedule impossible.

Constraints

  • •`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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def courses_are_possible(n: int, prerequisites: list[list[int]]) -> bool:
Sample call
courses_are_possible(3, [[2, 1], [1, 0]])
Language
Case 1
Input
(3, [[2, 1], [1, 0]])
Expected
True
Case 2
Input
(4, [[1, 0], [2, 3], [3, 2]])
Expected
False

Browse More Questions

Show the approach

Approach

Build a directed adjacency list from each course to its prerequisites. Nodes are unvisited, active on the DFS stack, or finished. Start an iterative DFS at every unvisited node. Each stack frame keeps the next outgoing edge index. An edge to an active node closes a directed cycle, so return false. An edge to a finished node is already resolved. Mark a node finished when all its outgoing edges have been examined.

A directed acyclic graph has no edge to an active ancestor, and reversing the dependency exploration's finish order gives a topological order along the graph edges. Because edges point from a course to its prerequisite, the finish order itself places prerequisites first. Repeated edges do not change either property. The loop succeeds immediately for zero courses.

Time complexity:
O(n + e)
Space complexity:
O(n + e)