Number of Connected Components in an Undirected Graph
Updated
Problem statement
You are given an undirected graph with n vertices labeled from 0 to n - 1 and an edge list edges. Return the number of connected components in the graph.
Function
countComponents(n: int, edges: int[][]) → intExamples
Example 1
n = 5edges = [[0,1],[1,2],[3,4]]return = 2The components are {0,1,2} and {3,4}.
Example 2
n = 5edges = [[0,1],[1,2],[2,3],[3,4]]return = 1Every vertex is connected through one chain.
Example 3
n = 4edges = []return = 4With no edges, each vertex forms its own component.
Constraints
1 <= n <= 2000.0 <= edges.length <= 5000.edges[i].length = 2.0 <= edges[i][0], edges[i][1] < n.- There are no self-loops or duplicate edges.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Count the Number of Complete ComponentsAmazon · PHONE SCREEN
- Number of ProvincesSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Path Existence in an Undirected GraphByteDance · PHONE SCREENByteDance interview questions
- Visible Profiles CountMicrosoft · OAMicrosoft interview questions
- Count Connected Components in an Undirected GraphNutanix · ONSITE INTERVIEWNutanix interview questions
- Connected GroupsVisa · OAVisa interview questions
- Path Existence in an Undirected GraphBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREENStripe interview questions