Find connected components in grid

Last updated: July 1, 2025

Quick Overview

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

Twilio
Coding & Algorithms
Software Engineer
Twilio
July 1, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Medium

10

11

2,988 solved


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

This coding problem is frequently asked during Technical Screen at Twilio. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Twilio 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
Tree structures and recursion
Dynamic programming and memoization
Sorting and searching
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 optimize the space complexity of your solution?
  • How would your solution change if the input was sorted?
  • Can you solve this iteratively instead of recursively (or vice versa)?
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 in a 2D grid, we can identify that the best pattern to use here is Depth-First Search (DFS). This is because we need to explore each connected comp...

Approach
  1. Initialize an empty list to hold the connected components. 2. Create a visited matrix of the same size as the grid to keep track of which cells have already been processed. 3. Iterate through each ...

Submit Your Answer
Markdown supported

Related Questions