Detect circular dependencies in a package manager

Quick Overview

A package manager must install every package after its dependencies. Given n packages and a list of dependency pairs, decide whether a circular dependency makes a full install impossible. It tests modeling dependencies as a directed graph and detecting cycles in time linear in packages plus dependencies.

Detect circular dependencies in a package manager

Company: Airtable

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are building a package manager that needs to install software packages in the correct order based on their dependencies. Each package may depend on zero or more other packages, and all of a package's dependencies must be installed before the package itself can be installed. You are given an integer `n` representing the number of packages, labeled from `0` to `n - 1`, and a 2D array `dependencies` where `dependencies[i] = [a, b]` indicates that package `a` depends on package `b` (`b` must be installed before `a`). Return `True` if there is a circular dependency that makes it impossible to install all packages, and `False` otherwise. ### Function Signature ```python def has_circular_dependency(n: int, dependencies: list[list[int]]) -> bool: pass ``` ### Rules - A circular dependency is a chain of "depends on" links that leads from a package back to itself, such as 0 depends on 1, 1 depends on 2, and 2 depends on 0. - A cycle anywhere makes the answer `True`, even in a group of packages that is not connected to the others (see Example 3). - A package that appears in no pair has no dependencies. ### Constraints - `1 <= n <= 10^4` - `0 <= len(dependencies) <= 10^5` - `len(dependencies[i]) == 2` - `0 <= a, b < n` - `a != b` - All dependency pairs are distinct. ### Examples ```text Input: n = 4, dependencies = [[1,0],[2,1],[3,2]] Output: False ``` Package 1 depends on 0, 2 depends on 1, and 3 depends on 2, so the install order 0, 1, 2, 3 works. No cycle exists. ```text Input: n = 3, dependencies = [[0,1],[1,2],[2,0]] Output: True ``` Package 0 depends on 1, 1 depends on 2, and 2 depends on 0. This forms a cycle, so none of the three can be installed first. ```text Input: n = 5, dependencies = [[1,0],[2,1],[3,4],[4,3]] Output: True ``` Packages 0, 1 and 2 install in that order, but packages 3 and 4 depend on each other, so neither can go first and the full install is impossible.

Overview: A package manager must install every package after its dependencies. Given n packages and a list of dependency pairs, decide whether a circular dependency makes a full install impossible. It tests modeling dependencies as a directed graph and detecting cycles in time linear in packages plus dependencies.

Airtable logo
Airtable
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are building a package manager that needs to install software packages in the correct order based on their dependencies. Each package may depend on zero or more other packages, and all of a package's dependencies must be installed before the package itself can be installed.

You are given an integer n representing the number of packages, labeled from 0 to n - 1, and a 2D array dependencies where dependencies[i] = [a, b] indicates that package a depends on package b (b must be installed before a).

Return True if there is a circular dependency that makes it impossible to install all packages, and False otherwise.

Function Signature

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

Rules

  • A circular dependency is a chain of "depends on" links that leads from a package back to itself, such as 0 depends on 1, 1 depends on 2, and 2 depends on 0.
  • A cycle anywhere makes the answer True , even in a group of packages that is not connected to the others (see Example 3).
  • A package that appears in no pair has no dependencies.

Constraints

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

Examples

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

Package 1 depends on 0, 2 depends on 1, and 3 depends on 2, so the install order 0, 1, 2, 3 works. No cycle exists.

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

Package 0 depends on 1, 1 depends on 2, and 2 depends on 0. This forms a cycle, so none of the three can be installed first.

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

Packages 0, 1 and 2 install in that order, but packages 3 and 4 depend on each other, so neither can go first and the full install is impossible.