Question bank › Graphs Dfs
Dsa Graphs Dfs Easy

Cycle Detection in a Graph

Write a program to determine if an undirected graph contains a cycle. The graph will be represented by a number of nodes and a list of edges. If a cycle exists, output 'CYCLE' otherwise output 'NO CYCLE'. Input format: - The first line contains two integers, n (1 ≤ n ≤ 1000) and m (0 ≤ m ≤ 10,000). - The next m lines contain the edges as two integers u and v. Output format: - 'CYCLE' if a cycle exists, 'NO CYCLE' otherwise. Example: Input: 4 4 1 2 2 3 3 4 4 1 Output: CYCLE

Key concepts

graphsdfscycle detection

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.