FastPrepNumber of Connected Components in an Undirected Graph
Problems/Number of Connected Components in an Undirected Graph

Number of Connected Components in an Undirected Graph

Google logoGoogle● MediumINTERNNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Google SWE intern report names Number of Connected Components in a live 45-minute technical interview. We rebuilt the undirected edge-list model, the labels, the input limits, the function signature, and the examples from the standard version of the problem. Our version is about 87% the same as the reported question.

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[][]) → int

Examples

Example 1

n = 5edges = [[0,1],[1,2],[3,4]]return = 2

The components are {0,1,2} and {3,4}.

Example 2

n = 5edges = [[0,1],[1,2],[2,3],[3,4]]return = 1

Every vertex is connected through one chain.

Example 3

n = 4edges = []return = 4

With 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

Interview reports that mention this question

  • Google PhD AI/ML New Grad Loop: 1 Experience
  • Google SWE and PhD Intern Coding: 14 Experiences (Part 3 of 3)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int countComponents(int n, int[][] edges) {
        // Write your code here.
    }
}
Checking account…