Find connected components in grid

Last updated: February 19, 2026

Quick Overview

Given a 2D grid consisting 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 that are 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.

LinkedIn
Coding & Algorithms
Software Engineer
LinkedIn
February 19, 2026
Software Engineer
Take-home Project
Coding & Algorithms
Easy

43

3

298 solved


Given a 2D grid consisting 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 that are 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.

LinkedIn uses this problem in the Take-home Project to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Edge cases and input validation
Data structure selection and trade-offs
Dynamic programming and memoization
Tree structures and recursion
Binary search and divide and conquer
Time and space complexity analysis
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?
  • Can you solve this in a single pass?
  • How would you parallelize this solution?
  • What if the input doesn't fit in memory?
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

In this problem, we need to identify all connected components of land (1s) in a 2D grid where connections are made through adjacent cells (up, down, left, right). The two-pointer technique or Depth-Fi...

Approach

We will utilize a Depth-First Search (DFS) algorithm to traverse the grid and identify connected components. Here’s the step-by-step approach:

  1. Initialize an empty list components to store the con...

Submit Your Answer
Markdown supported

Related Questions