Number of Connected Components in an Undirected Graph
Medium · Graphs · about 15 minutes
Asked at LinkedIn, Google, Amazon, Meta, Microsoft.
An undirected graph has nodes 0..n-1 and the edge list edges. Count how many separate pieces it falls into, where each piece is a set of nodes linked to each other and to nothing outside.