Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Connected Components

Python data engineering interview problem. Difficulty: advanced. Pattern: Graphs. About 18 minutes. Part of the Pro drill bank.

Count connected components in an undirected graph. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement count_components(n: int, edges: list) -> int for nodes 0..n-1.

Constraints

  • Graph is undirected.
  • Isolated nodes count as components.

Examples

Input: count_components(5, [[0, 1], [1, 2], [3, 4]]) Output: 2 Two components (and no isolate extras beyond those).

Topics: lakebench, python, union-find, components.

More Python interview questions · All interview problems · Learn data engineering

advanced

Connected Components

Interview-style drill: Count connected components in an undirected graph.

Implement `count_components(n: int, edges: list) -> int` for nodes `0..n-1`. Example: `5, [[0, 1], [1, 2], [3, 4]]` → `2`. Keep the harness.