Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to detect a cycle in a directed graph using the DFS algorithm.

Hard

Problem Statement

Write a function to detect a cycle in a directed graph using the DFS algorithm.

Sample Input

Graph: { 0: [1], 1: [2], 2: [0], 3: [4], 4: [] }

Sample Output

Cycle detected: true

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
function detectCycle(graph: { [key: number]: number[] }): boolean { // Your logic here }
Log in or sign up to run or submit your code.Log inSign up
Input
Graph: { 0: [1], 1: [2], 2: [0], 3: [4], 4: [] }
Expected Output
Cycle detected: true