Number of Islands

Last updated: June 17, 2026

Quick Overview

Given a 2D grid of '1's (land) and '0's (water), count the number of islands. Tests BFS/DFS on grids, one of Google's most frequently asked problems.

Google
Coding & Algorithms
Software Engineer
Google
June 17, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

384

0

54 solved


Given a 2D grid of '1's (land) and '0's (water), count the number of islands. Tests BFS/DFS on grids, one of Google's most frequently asked problems.

How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem of counting the number of islands in a 2D grid is best solved using Depth-First Search (DFS) or Breadth-First Search (BFS). Here, the grid is composed of '1's representing land and '0's re...

Approach
  1. Initialize a counter to track the number of islands.
  2. Iterate through each cell in the 2D grid.
  3. When a '1' is found, increment the island counter.
  4. Perform a DFS or BFS from that cell to vis...

Submit Your Answer
Markdown supported

Related Questions