Count connected components in matrix

Last updated: January 16, 2026

Quick Overview

Given a binary matrix where 1s represent land and 0s represent water, write a function to count the number of connected components of land. Two land cells are considered connected if they are adjacent horizontally or vertically. The function should take the matrix as input and return an integer representing the number of connected components.

Grubhub
Coding & Algorithms
Software Engineer
Grubhub
January 16, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

34

14

4,658 solved


Given a binary matrix where 1s represent land and 0s represent water, write a function to count the number of connected components of land. Two land cells are considered connected if they are adjacent horizontally or vertically. The function should take the matrix as input and return an integer representing the number of connected components.

This coding problem is frequently asked during Technical Screen at Grubhub. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Grubhub 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
Sorting and searching
Tree structures and recursion
Graph algorithms and traversal
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Edge cases and input validation
Hash maps and frequency counting
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
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • What is the worst-case input for your solution?
  • How would you parallelize this solution?
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 the number of connected components in a binary matrix, we can utilize Depth First Search (DFS). This approach is suitable here because the problem involves traversing ...

Approach
  1. Initialize a variable count to zero to keep track of connected components.
  2. Loop through each cell in the matrix.
    • If a cell contains '1', it indicates a new connected component. Increm...

Submit Your Answer
Markdown supported

Related Questions