Island Area and Water Boundary
Reported by candidates from Waymo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Input size is the first thing to check on this Waymo OA, reported September 2026. The grid tops out at 100 by 100, so 10,000 cells. That rules out re-scanning the whole grid for every island or every water cell, but a single flood fill per island is cheap. It's a BFS or DFS island problem with one twist: you also have to count distinct water neighbors per island. If you blank on the twist, StealthCoder is the invisible safety net running during the live OA. The rest is bookkeeping.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Scan row-major. When you hit an unvisited 1, start a BFS. Count land cells as you pop them. For each neighbor inside the grid, if it's land and unvisited, enqueue it. If it's water, add its coordinates to a set local to this island. When the BFS ends, push [area, set.size] to the result. Row-major discovery gives you the required order for free. The pitfall is a global visited set for water. Don't do that, because one water cell can border two islands and must count for both. Use a fresh set per island, or a stamp array holding the island id. Skip out-of-bounds neighbors, since edges don't count. Total work is O(rows*cols). If the live OA freezes you on the per-island set idea, StealthCoder can hand you the clean version in real time.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Island Area and Water Boundary cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Waymo's OA.
Waymo reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Island Area and Water Boundary FAQ
What's the trick in the Waymo Island Area and Water Boundary problem?+
Use a fresh water set for each island. Flood fill the island with BFS or DFS, add every in-grid 0 neighbor to that set, and the boundary is the set size. A global water set breaks because shared water cells must count once per island.
How hard is this one really?+
It's a standard connected components problem with one extra counter. If you've done number of islands, you're 80 percent there. The only real risk is the water dedup logic and the bounds checks at the grid edges.
BFS or DFS, which should I use?+
Either works. The grid is at most 100 by 100, so recursion depth could reach 10,000 in a snake-shaped island. BFS with a queue avoids stack overflow worries, so it's the safer pick, especially in languages with shallow recursion limits.
How do I get the output order right?+
Scan rows top to bottom and columns left to right. Start a fill only on unvisited land cells. Append each island's pair right when its fill finishes. That naturally matches first-encountered order, so no sorting is needed.
How do I prepare in 48 hours?+
Write number of islands from scratch, then add the per-island water set and test against the three examples. Check a single-cell grid, an all-water grid, and an all-land grid. Those edge cases catch most bugs, and it's about an hour of work.