FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

WhatNot logoWhatNot● MediumNEW GRADFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Whatnot candidates name this task. A new grad got it in late 2024 as one of two quick questions in the first coding round that came after the Karat screen. A full-time applicant had it early in 2024, in a first-round technical screen. An experienced engineer got it in the third of six rounds, written up in January 2025.

All three name the task and nothing more. Here, land is '1', water is '0', only up, down, left and right neighbours connect, and the size limits are the common ones for this task.

Our version is about 90% the same as the reported question.

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[][]) → int

Examples

Example 1

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

Writing 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 = 5
grid
012010110102101

Every 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 = 1
grid
01234011111100001211101310111

All 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 = 0
grid
01200001000

There is no land at all, so there are no islands.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[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

Interview reports that mention this question

  • Whatnot SWE New Grad Second Technical Interview: 8 Experiences

WhatNot interview experiences

  • Whatnot interviews · October 2026
  • Whatnot Experienced SWE Phone Screen Questions: 2 Experiences
  • Whatnot SWE Interview Questions: 6 Experiences
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public int numIslands(char[][] grid) {
        // write your code here
    }
}
Checking account…