Number of Islands
Updated
Problem statement
You are given an m x n grid of characters. Each cell is either '1' (land) or '0' (water).
Two land cells are connected when they are next to each other up, down, left or right. Cells that only touch at a corner are not connected. An island is a group of land cells that are all connected to each other, directly or through other land cells, and that is as large as possible. Everything outside the grid is water.
Return how many islands the grid contains.
Function
numIslands(grid: char[][]) → intExamples
Example 1
grid = [["1","1","0","0"],["1","0","0","1"],["0","0","1","1"],["0","0","0","0"]]return = 2Writing a cell as (row, column), the land cells (0, 0), (0, 1) and (1, 0) touch each other and form one island. The land cells (1, 3), (2, 2) and (2, 3) form a second island. No cell of one group is next to a cell of the other.
Example 2
grid = [["1","0","1"],["0","1","0"],["1","0","1"]]return = 5Every land cell touches the others only at a corner. Corners do not connect cells, so each of the five land cells is its own island.
Example 3
grid = [["1","1","1","1","1"],["0","0","0","0","1"],["1","1","1","0","1"],["1","0","1","1","1"]]return = 1All the land cells form one winding path: across the top row, down the right edge, left along the bottom row to column 2, up one cell, left along row 2, and down to the bottom-left corner. One path is one island.
Example 4
grid = [["0","0","0"],["0","0","0"]]return = 0There is no land at all, so there are no islands.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j]is'0'or'1'.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
- Count the IslandsAdobe · PHONE SCREENAdobe interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Count Islands with Eight-Direction AdjacencyAmazon · PHONE SCREEN
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Maximum Number of Fish in a GridGlobalization Partners · ONSITE INTERVIEW