Mid
Detailed answer
Graphs / Grid DFS
DSA & Coding Interviews
How do you solve Number of Islands?
Short answer: Scan the grid. When you find a '1', increment island count and DFS/BFS to sink (mark visited) the entire connected land component. Count how many times you start a flood fill.
Complexity
Time O(rows * cols), Space O(rows * cols) worst-case recursion/queue.
Edge cases to mention
- Empty grid
- All water
- Diagonal connectivity? (usually 4-directional only)
Common follow-ups
- Max area of island
- Number of distinct islands
- Surrounded regions
Clarify 4-direction vs 8-direction before coding.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png