‹ DS&A interview · Socratic
DSA · Graph / DFS-BFS · #06

Number of Islands

Module 11 · difficulty 3/5·30:00starts on first keystroke

Given an `m x n` 2D binary grid which represents a map of `'1'`s (land) and `'0'`s (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically (NOT diagonally). You may assume all four edges of the grid are surrounded by water.

Examples
  • grid = [["1","1","0"],["1","1","0"],["0","0","1"]] 2
  • grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]] 1
  • grid = [] 0
Constraints
  • · m == grid.length
  • · n == grid[i].length
  • · 1 <= m, n <= 300
  • · grid[i][j] is '0' or '1'.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.