FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Expedia software engineering intern reports mention this task. Two from 2024 list the well-known number of islands problem among the final interview questions, and one of them says it was a modified version without saying how. A 2014 onsite report describes the same counting task on a matrix of ones and zeros without naming it.

The reports give no input format, examples or limits, and none says whether cells that touch only at a corner are connected. Here the grid holds 0 for water and 1 for land. Land joins only through shared sides. The grid is at most 200 by 200. We do not include the modified version.

Our version is about 85% the same as the reported questions.

You are given a map as a grid grid with m rows and n columns. Each cell is 1 for land or 0 for water.

Two land cells are connected when they share a side: one is directly above, below, left or right of the other. Cells that touch only at a corner are not connected. An island is a group of land cells that are connected to each other, directly or through other land cells, and not connected to any other land. Everything outside the grid is water.

Return the number of islands.

Function

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

Examples

Example 1

grid = [[1,1,0,1],[0,1,0,1],[1,0,0,0],[1,1,0,1]]return = 4
grid
012301101101012100031101

Write (r, c) for row r and column c, counting from 0. The islands are:

  • (0, 0), (0, 1) and (1, 1);
  • (0, 3) and (1, 3);
  • (2, 0), (3, 0) and (3, 1);
  • (3, 3) alone.

Cells (1, 1) and (2, 0) touch only at a corner, so they are in different islands. The answer is 4.

Example 2

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

The two columns of land are not next to each other at the top, but both reach the bottom row, and the bottom row joins them. So all seven land cells form one island.

Example 3

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

There is no land, so there are no islands.

Constraints

  • m == grid.length
  • n == grid[i].length for every row, so all rows have the same length.
  • 1 <= m, n <= 200
  • Each grid[i][j] is 0 or 1.

More Expedia Onsite Interview questions

  • Scramble StringONSITE INTERVIEW · Seen Sep 2026
  • Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
  • Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
  • Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
  • Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
  • Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
  • Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
  • Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
  • Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
  • Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024

Similar questions at other companies

  • Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
  • Count the IslandsAdobe · PHONE SCREENAdobe interview questions
  • Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Maximum Number of Fish in a GridGlobalization Partners · ONSITE INTERVIEW
  • Max Area of IslandGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
  • Max Area of IslandBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Maximum Area of IslandSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
  • Count Connected IslandsTwitch · ONSITE INTERVIEWTwitch interview questions

Interview reports that mention this question

  • Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
  • Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)

Expedia interview experiences

  • Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
  • Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
  • Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
  • Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int countIslands(int[][] grid) {
        // Write your code here.
    }
}
Checking account…