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.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details