Skip to main content

88. Number of Islands

mediumAsked at Vercel

Count the number of islands in a 2D grid of '1' (land) and '0' (water). Vercel asks this for the canonical grid-DFS / connected-components pattern — same shape as counting disjoint edge-failure clusters in their network monitoring.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; DFS expected.
  • Blind (2026-Q1)Listed in Vercel routing engineer screen.

Problem

Given an m x n 2D binary grid 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. You may assume all four edges of the grid are all surrounded by water.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is '0' or '1'.

Examples

Example 1

Input
grid = [['1','1','1','1','0'],['1','1','0','1','0'],['1','1','0','0','0'],['0','0','0','0','0']]
Output
1

Example 2

Input
grid = [['1','1','0','0','0'],['1','1','0','0','0'],['0','0','1','0','0'],['0','0','0','1','1']]
Output
3

Approaches

1. DFS with visited set

Walk grid; on each unvisited 1, DFS to mark all connected 1s; increment count.

Time
O(m*n)
Space
O(m*n)
// Works; uses extra space for visited.

Tradeoff: Extra space.

2. DFS with in-place sink (optimal)

When DFS reaches a land cell, set it to '0' to mark visited. Saves the visited set.

Time
O(m*n)
Space
O(m*n) recursion in worst case
function numIslands(grid) {
  const m = grid.length, n = grid[0].length;
  let count = 0;
  function dfs(r, c) {
    if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] !== '1') return;
    grid[r][c] = '0';
    dfs(r + 1, c); dfs(r - 1, c); dfs(r, c + 1); dfs(r, c - 1);
  }
  for (let r = 0; r < m; r++) {
    for (let c = 0; c < n; c++) {
      if (grid[r][c] === '1') {
        count++;
        dfs(r, c);
      }
    }
  }
  return count;
}

Tradeoff: O(m*n) time, no extra visited storage. Mutates the input — confirm that's allowed.

Vercel-specific tips

Vercel grades the in-place sink trick. Bonus signal: asking 'can I mutate the grid?' before coding. If not, fall back to a visited Set. Mention BFS as an alternative for very deep recursion (worst case is m*n stack frames on a snake-shaped island).

Common mistakes

  • Forgetting to mark visited — infinite recursion.
  • Counting 0s instead of 1s — opposite answer.
  • Comparing grid[r][c] to 1 (number) instead of '1' (string) — LC's grid is character-strings.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Max Area of Island (LC 695) — sum island sizes.
  • Number of Distinct Islands (LC 694).
  • Surrounded Regions (LC 130) — reverse pattern.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why mutate the grid?

Saves O(m*n) extra space for a visited set. If the caller can't afford the mutation, use a Set.

BFS vs DFS — which is preferred?

Both are O(m*n). BFS uses an explicit queue (heap-allocated); DFS uses recursion stack. On a snake-shaped island, DFS can blow the stack — BFS is safer for very large grids.

Practice these live with InterviewChamp.AI

Drill Number of Islands and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →