core practice · coding problem
Number of islands
Given a grid of '1' (land) and '0' (water), count the connected land masses (4-directionally).
The mechanism
Most graph problems are one of a few templates: reachability/components (DFS/BFS), shortest path (BFS for unweighted, Dijkstra for weighted), ordering (topological sort), or connectivity (union-find). The skill is recognizing the template and modeling the input as nodes and edges.
Expected evidence
A single integer — the island count.
Try this drill in the practice workspace →
Hints when you need them
- Each unvisited '1' starts a new island; flood-fill it with DFS or BFS.
- Mark cells visited as you go to avoid recounting.
Watch for these mistakes
- No visited set — revisiting nodes and looping forever
- Using DFS for shortest path on an unweighted graph instead of BFS
- Not handling disconnected components