The Number of Islands problem gives you a 2D binary grid where '1' represents land and '0' represents water. Count the number of islands — groups of connected '1' cells (4-directional connectivity). This Number of Islands coding problem is the most widely asked graph traversal problem in all of technical interviews.
This problem is asked by virtually every major tech company: Google, Amazon, Meta, Microsoft, Apple, Bloomberg, Uber, and hundreds more. It is the definitive test of BFS/DFS on grids and serves as a template for dozens of related problems. The array, matrix, union find, BFS, and DFS interview pattern is the cornerstone of grid problem-solving.
DFS or BFS flood-fill. For each unvisited '1' cell, run a DFS/BFS that marks all connected land cells as visited ('0' or a separate visited marker). Each DFS/BFS invocation = one island. Count total invocations.
Union-Find alternative: Initialize each '1' cell as its own component. For each pair of adjacent '1' cells, union them. Count final components.
Grid:
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
Number of Islands must be mastered completely. Implement DFS, iterative BFS, and Union-Find solutions. Know when to use each: DFS is simplest to code, BFS avoids stack overflow, Union-Find handles dynamic updates. The flood-fill template — for each cell of target type, DFS to mark its connected region — applies to 50+ grid problems. Practice until you can implement all three versions in under 10 minutes combined.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Max Area of Island | Medium | Solve | |
| Surrounded Regions | Medium | Solve | |
| Count Sub Islands | Medium | Solve | |
| Number of Closed Islands | Medium | Solve | |
| Detect Cycles in 2D Grid | Medium | Solve |