As reported: ServiceNow Staff Engineer loop (Data Analytics team, Hyderabad), round 1, Oct 2025: a discussion of current work and projects, then finding the disconnected components of a graph; the candidate used DFS.
There are n nodes labelled 0…n−1 and a list of undirected edges. Return the number of connected components.
This is LeetCode 323 — Number of Connected Components in an Undirected Graph.
Example
n = 5, edges = [[0,1],[1,2],[3,4]] -> 2
n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] -> 1
Constraints: 1 ≤ n ≤ 2000; 0 ≤ edges.length ≤ 5000; no repeated edges or self-loops.
Code runner format: line 1 n, line 2 edges. Implement countComponents(n, edges); the harness prints the count.