Question Bank
/

Graph Theory Essentials

Graph Theory Essentials

Interview-grade prompts on BFS/DFS, shortest paths, connectivity, and cycle detection across directed and undirected graphs.

Question Bank
Hard
Python
graphs
graph-algorithms
interview-prep
algorithms

607 views

8

Detect whether a directed graph has a cycle. Return true if so. The graph is an adjacency list.

Examples

Example 1:

Input: n = 3, adj = [[1], [2], [0]]
Output: True
Explanation: DFS with three colors (WHITE, GRAY, BLACK). The vertex 0 -> 1 -> 2 -> 0 path revisits a GRAY vertex, a back-edge that signals a cycle.

Example 2:

Input: n = 3, adj = [[1], [2], []]
Output: False
Explanation: A simple chain 0 -> 1 -> 2 has no back-edges; every vertex becomes BLACK without ever encountering a GRAY successor.

4 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium