FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

The Walt Disney Company logoThe Walt Disney Company● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The QA interview report names Number of Islands and describes BFS and DFS, first in Python and then in Java. The original grid representation, connectivity, bounds and mutation policy are unavailable. This practice uses a rectangular 0/1 integer grid, four-directional connections, empty grids and allowed mutation, bounded by 30 rows and columns. Both traversals are explained in every editorial language. About 85% of the reported task scope is represented, as an editorial estimate rather than original-code similarity.

Return the number of islands in a rectangular binary grid. An island is a maximal group of land cells connected through shared horizontal or vertical edges. Diagonal contact does not connect islands.

For this exercise, integer 1 means land and 0 means water. The outside of the grid is water. An empty grid or zero-column grid has zero islands. You may modify the input grid.

Function

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

Examples

Example 1

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

The cells [0,0], [0,1] and [1,1] form one island. Cells [2,0] and [2,2] each form a separate island, for three total.

Example 2

grid = [[0,0],[0,0]]return = 0

Every cell is water, so the count is zero.

Constraints

  • 0 <= grid.length <= 30
  • 0 <= grid[r].length <= 30
  • All rows have the same length.
  • 0 <= grid[r][c] <= 1

More The Walt Disney Company problems

  • Find All Paths for a Name in a File SystemPHONE SCREEN · Seen Nov 2026
  • Resizable Array-Backed Integer SetPHONE SCREEN · Seen Sep 2026
  • Shortest Path with Directional Move CostsPHONE SCREEN · Seen Sep 2026
All The Walt Disney Company interview questionsSee The Walt Disney Company hiring insights
Saved
class Solution {
    public int numIslands(int[][] grid) {
        // Write your code here
    }
}
Checking account…