FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

Google logoGoogle● MediumINTERNFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Google SWE Intern report in the 2026 recruiting cycle explicitly names Number of Islands as a graph-round problem. The canonical four-directional grid task matches the report at about 95%.

You are given an m x n grid of characters. A cell containing '1' is land and a cell containing '0' is water.

An island is a maximal group of land cells connected horizontally or vertically. Treat the area outside all four grid edges as water.

Return the number of islands in the grid.

Function

numIslands(grid: char[][]) → int

Examples

Example 1

grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]return = 1

Every land cell belongs to one four-directionally connected component.

Example 2

grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]return = 3

The grid contains three separate land components.

Constraints

  • 1 <= m, n <= 300.
  • m == grid.length and n == grid[i].length.
  • Every cell is either '0' or '1'.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int numIslands(char[][] grid) {
        // Write your code here.
    }
}
Checking account…