FastPrepIsland Area and Water Boundary
Problems/Island Area and Water Boundary

Island Area and Water Boundary

Waymo logoWaymo● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The original task allows the island pairs in any order. This practice version uses row-major island discovery for deterministic grading; the judged core task matches the visible source at about 99%.

You are given a non-empty rectangular binary matrix grid. 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.

For every island, compute a pair [area, waterBoundary]:

  • area is the number of land cells in the island.
  • waterBoundary is the number of distinct in-grid water cells horizontally or vertically adjacent to at least one cell in that island. Count a water cell at most once for the same island, even when it touches multiple island cells. Do not count positions outside the matrix.

Return the pairs in the order in which their islands are first encountered while scanning grid from top to bottom and left to right.

Function

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

Examples

Example 1

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

The three land cells form one island. Its five distinct adjacent water cells are [0,0], [0,2], [1,2], [2,0], and [2,1].

Example 2

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

Each corner land cell is a separate one-cell island with two adjacent water cells.

Example 3

grid = [[1,1,0,0,0],[1,0,0,1,1],[0,0,0,1,0],[0,1,1,0,0]]return = [[3,3],[3,6],[2,4]]

The row-major scan discovers islands with areas 3, 3, and 2. Their distinct water-boundary counts are 3, 6, and 4.

Constraints

  • 1 <= grid.length <= 100.
  • 1 <= grid[i].length <= 100.
  • Every row has the same length.
  • Every cell is either 0 or 1.

More Waymo problems

  • Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
  • Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
  • Linked Perception HistoryONSITE INTERVIEW · Seen Sep 2026
All Waymo interview questionsSee Waymo hiring insights
Saved
class Solution {
    public int[][] islandAreaAndWaterBoundary(int[][] grid) {
        // Write your solution here.
    }
}
Checking account…