Connected Components & Flood Fill
Count islands and label regions.
Connected Components & Flood Fill is a free Coding Interview Prep lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Component Is
A connected component is a group of nodes you can all reach from one another. A graph can hold several separate groups. 🧩
Counting Components
To count components, run a traversal from every unvisited node. Each fresh start marks one whole new group.
Loop Over All Nodes
Walk through nodes 1 to n. When you find one still unvisited, you have discovered a new component to explore.
for s in range(1, n + 1):
if not visited[s]:
bfs_or_dfs(s)
count += 1One Traversal Per Group
That inner BFS or DFS marks the entire component visited, so the outer loop skips it the next time around.
Grids Are Graphs Too
A 2D grid is a hidden graph: each cell is a node linked to its neighbors. This unlocks the classic flood fill idea. 🗺️
The Four Directions
From a cell you usually move up, down, left, and right. Store those moves as direction vectors to keep code clean.
dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]Stay Inside the Grid
Before stepping, check the new row and column are in bounds. Skipping this read causes index errors or wrong answers.
if 0 <= nr < rows and 0 <= nc < cols:
passFlood Fill One Region
Flood fill starts at a cell and spreads to every connected same-type cell, just like the paint-bucket tool.
Counting Islands
To count islands, scan the grid; at each new land cell, flood fill its whole island and add one to the count.
if grid[r][c] == '1' and not seen[r][c]:
flood(r, c)
islands += 1Labeling Regions
You can store a label per cell during the fill. Later you instantly know which region any cell belongs to.
Linear in Grid Size
Every cell is visited once, so flood fill over a grid runs in O(rows times cols). That comfortably fits contest limits.
Quick Check
How do you count connected components?
Recap
You count components by traversing from each unvisited node, and use flood fill on grids to label regions and count islands. 🎉
Frequently asked questions
Is the “Connected Components & Flood Fill” lesson free?
Yes — the full text of “Connected Components & Flood Fill” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Connected Components & Flood Fill”?
Count islands and label regions. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Connected Components & Flood Fill” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Adjacency Lists from Input
- BFS for Shortest Unweighted Paths
- DFS, Recursion & Iterative Stacks
- Connected Components & Flood Fill