Find connected components in grid

Last updated: October 16, 2025

Quick Overview

Given a 2D grid consisting of '1's (land) and '0's (water), write a function to find all the connected components of '1's in the grid. A connected component is defined as a group of adjacent '1's connected horizontally or vertically. The function should return a list of lists, where each inner list contains the coordinates of the cells in a connected component.

Brex
Coding & Algorithms
Machine Learning Engineer
Brex
October 16, 2025
Machine Learning Engineer
Onsite
Coding & Algorithms
Medium

1

10

655 solved


Given a 2D grid consisting of '1's (land) and '0's (water), write a function to find all the connected components of '1's in the grid. A connected component is defined as a group of adjacent '1's connected horizontally or vertically. The function should return a list of lists, where each inner list contains the coordinates of the cells in a connected component.

Coding interviews at Brex focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

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
Tree structures and recursion
Graph algorithms and traversal
Time and space complexity analysis
Binary search and divide and conquer
Edge cases and input validation
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)?
  • How would you test this solution thoroughly?
  • How would you parallelize this solution?
  • How would you modify your solution to handle streaming input?
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 finding connected components of '1's in a 2D grid, we can recognize that this is a graph traversal issue. Each '1' can be considered a node, and connections (edges) exist betwe...

Approach

We will implement a DFS algorithm to explore the grid. Here are the steps:

  1. Iterate through each cell in the grid. If we encounter a '1', it indicates the start of a new connected component.
  2. From...

Submit Your Answer
Markdown supported

Related Questions