Check whether task dependencies contain a cycle

Quick Overview

Given n tasks and a list of dependency pairs, decide whether the dependencies contain a cycle that makes it impossible to finish every task. The problem tests modeling dependencies as a directed graph, handling unconnected groups and shared prerequisites, and keeping the check linear in tasks plus edges.

Check whether task dependencies contain a cycle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` tasks labeled from `0` to `n - 1`, and a list `dependencies` where `dependencies[i] = [a, b]` means task `a` depends on task `b`: task `b` must be completed before task `a` can start. Return `True` if the dependencies contain a cycle, which makes it impossible to complete all tasks, and `False` otherwise. ### Function Signature ```python def has_cycle(n: int, dependencies: list[list[int]]) -> bool: pass ``` ### Rules - Each pair `[a, b]` is one directed dependency: `a` waits for `b`. - A cycle is a chain of dependencies that leads from a task back to the same task. A task that depends on itself, such as `[2, 2]`, is a cycle of length one. - The tasks may form several unconnected groups. A cycle in any group makes the answer `True`. - A task that appears in no pair has no dependencies. ### Constraints - `1 <= n <= 10^4` - `0 <= len(dependencies) <= 10^5` - `len(dependencies[i]) == 2` - `0 <= dependencies[i][0], dependencies[i][1] < n` - All dependency pairs are unique. ### Examples ```text Input: n = 4, dependencies = [[1,0],[2,1],[3,2]] Output: False ``` Task 1 waits for task 0, task 2 waits for task 1, and task 3 waits for task 2. Running the tasks in the order 0, 1, 2, 3 satisfies every dependency, so there is no cycle. ```text Input: n = 3, dependencies = [[0,1],[1,2],[2,0]] Output: True ``` Task 0 waits for task 1, task 1 waits for task 2, and task 2 waits for task 0. Following the dependencies from task 0 leads back to task 0, so none of the three tasks can ever start. ```text Input: n = 4, dependencies = [[1,0],[2,0],[3,1],[3,2]] Output: False ``` Tasks 1 and 2 both wait for task 0, and task 3 waits for both 1 and 2. Two dependency paths meet at task 3 without looping back, so the order 0, 1, 2, 3 is valid.

Overview: Given n tasks and a list of dependency pairs, decide whether the dependencies contain a cycle that makes it impossible to finish every task. The problem tests modeling dependencies as a directed graph, handling unconnected groups and shared prerequisites, and keeping the check linear in tasks plus edges.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given n tasks labeled from 0 to n - 1, and a list dependencies where dependencies[i] = [a, b] means task a depends on task b: task b must be completed before task a can start.

Return True if the dependencies contain a cycle, which makes it impossible to complete all tasks, and False otherwise.

Function Signature

def has_cycle(n: int, dependencies: list[list[int]]) -> bool:
    pass

Rules

  • Each pair [a, b] is one directed dependency: a waits for b .
  • A cycle is a chain of dependencies that leads from a task back to the same task. A task that depends on itself, such as [2, 2] , is a cycle of length one.
  • The tasks may form several unconnected groups. A cycle in any group makes the answer True .
  • A task that appears in no pair has no dependencies.

Constraints

  • 1 <= n <= 10^4
  • 0 <= len(dependencies) <= 10^5
  • len(dependencies[i]) == 2
  • 0 <= dependencies[i][0], dependencies[i][1] < n
  • All dependency pairs are unique.

Examples

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

Task 1 waits for task 0, task 2 waits for task 1, and task 3 waits for task 2. Running the tasks in the order 0, 1, 2, 3 satisfies every dependency, so there is no cycle.

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

Task 0 waits for task 1, task 1 waits for task 2, and task 2 waits for task 0. Following the dependencies from task 0 leads back to task 0, so none of the three tasks can ever start.

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

Tasks 1 and 2 both wait for task 0, and task 3 waits for both 1 and 2. Two dependency paths meet at task 3 without looping back, so the order 0, 1, 2, 3 is valid.