Enumerate All Simple Directed Cycles

Quick Overview

Enumerate distinct simple directed cycles using a canonical representation that handles rotations, opposite directions, and self-loops.

Enumerate All Simple Directed Cycles

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a directed graph as an adjacency list, return every distinct simple directed cycle. For this practice version, a simple directed cycle follows directed edges back to its starting vertex without repeating any other vertex. Represent a cycle as an array of its vertices without repeating the starting vertex at the end. The last vertex must have an edge to the first. A self-loop is a one-vertex cycle. ### Input - `graph`: an array of adjacency lists. Vertices are labeled `0` through `graph.length - 1`; `graph[u]` lists the vertices reachable by one directed edge from `u`. ### Output Return the cycles in canonical form: - Rotate each cycle so its smallest vertex label appears first. - List each such cycle once. Rotations of a sequence represent the same cycle. - Preserve edge direction. Reversing a sequence does not generally represent the same directed cycle. - Sort the resulting arrays lexicographically by their vertex sequences, using the shorter sequence first when one is a prefix of another. ### Constraints and Edge Cases - For this practice version, `1 <= graph.length <= 8`. - Adjacency lists may be unsorted but contain no repeated neighbors. - The graph may be disconnected and may contain self-loops. - If the graph contains no directed cycles, return an empty array. ### Example 1 ```text graph = [[1], [2], [0,1], []] output = [[0,1,2], [1,2]] ``` The cycles `[1,2,0]` and `[2,0,1]` are rotations of `[0,1,2]`, so they are not additional results. ### Example 2 ```text graph = [[0,1,2], [0,2], [0,1]] output = [[0], [0,1], [0,1,2], [0,2], [0,2,1], [1,2]] ``` Both `[0,1,2]` and `[0,2,1]` are valid because their directed edges exist, and they are distinct. The self-loop contributes `[0]`.

Overview: Enumerate distinct simple directed cycles using a canonical representation that handles rotations, opposite directions, and self-loops.

|Home/Coding & Algorithms/ByteDance
ByteDance logo
ByteDance
Aug 23, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Given a directed graph as an adjacency list, return every distinct simple directed cycle.

For this practice version, a simple directed cycle follows directed edges back to its starting vertex without repeating any other vertex. Represent a cycle as an array of its vertices without repeating the starting vertex at the end. The last vertex must have an edge to the first. A self-loop is a one-vertex cycle.

Input

  • graph : an array of adjacency lists. Vertices are labeled 0 through graph.length - 1 ; graph[u] lists the vertices reachable by one directed edge from u .

Output

Return the cycles in canonical form:

  • Rotate each cycle so its smallest vertex label appears first.
  • List each such cycle once. Rotations of a sequence represent the same cycle.
  • Preserve edge direction. Reversing a sequence does not generally represent the same directed cycle.
  • Sort the resulting arrays lexicographically by their vertex sequences, using the shorter sequence first when one is a prefix of another.

Constraints and Edge Cases

  • For this practice version, 1 <= graph.length <= 8 .
  • Adjacency lists may be unsorted but contain no repeated neighbors.
  • The graph may be disconnected and may contain self-loops.
  • If the graph contains no directed cycles, return an empty array.

Example 1

graph = [[1], [2], [0,1], []]
output = [[0,1,2], [1,2]]

The cycles [1,2,0] and [2,0,1] are rotations of [0,1,2], so they are not additional results.

Example 2

graph = [[0,1,2], [0,2], [0,1]]
output = [[0], [0,1], [0,1,2], [0,2], [0,2,1], [1,2]]

Both [0,1,2] and [0,2,1] are valid because their directed edges exist, and they are distinct. The self-loop contributes [0].

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...