Count connected components in array

Last updated: August 22, 2025

Quick Overview

Given an array of integers where each integer represents a node in a graph, count the number of connected components in the graph. A connected component is defined as a subset of nodes such that there is a path between any two nodes in this subset. Your function should take the array as input and return the total count of connected components as an integer.

Amazon
Coding & Algorithms
Software Engineer
Amazon
August 22, 2025
Software Engineer
Phone Screen
Coding & Algorithms
Medium

19

3

641 solved


Given an array of integers where each integer represents a node in a graph, count the number of connected components in the graph. A connected component is defined as a subset of nodes such that there is a path between any two nodes in this subset. Your function should take the array as input and return the total count of connected components as an integer.

This coding problem is frequently asked during Phone Screen at Amazon. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Amazon expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Binary search and divide and conquer
Time and space complexity analysis
Tree structures and recursion
Graph algorithms and traversal
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you modify your solution to handle streaming input?
  • Can you optimize the space complexity of your solution?
  • What is the worst-case input for your solution?
  • What happens if the input contains duplicates?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To solve the problem of counting connected components in a graph represented by an array of integers, we recognize that this is fundamentally a graph traversal problem. Each integer can be treated as ...

Approach
  1. Create a graph representation: Using a dictionary, map each integer to its connected nodes. For example, if the array is [1, 2, 1, 2, 3], we could create connections like `{1: [2], 2: [1], 3:...

Submit Your Answer
Markdown supported

Related Questions