Question bank › Graphs Dfs
Dsa Graphs Dfs Easy

Disconnected Components Counter

Given an undirected graph represented by an adjacency list, count the number of disconnected components in the graph. The input will start with an integer N, the number of nodes (0-indexed from 0 to N-1), followed by M pairs of integers representing edges. Each edge connects two nodes. For example, the input: 5 0 1 0 2 3 4 represents a graph with 5 nodes and 3 edges, where nodes 0, 1, and 2 form one component, and nodes 3 and 4 form another, resulting in 2 components overall. Output the number of disconnected components in the graph.

Key concepts

graphsdfsconnected components

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.