Number of Islands
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged Number of Islands in a September 2026 report, and the whole thing hinges on one structure: a visited marker over the grid, driven by a stack or queue (or plain recursion). If your OA is in the next day or two, this is a connected-components problem on a grid, nothing more exotic. Count how many separate groups of '1' cells touch horizontally or vertically. The logic is short, but the edge cases trip people who rush. StealthCoder sits invisibly on your screen during the live assessment as a safety net if your mind goes blank on the traversal.
The problem
You are given an m x n grid of characters. 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. Treat the area outside all four grid edges as water. Return the number of islands in the grid. Function numIslands(grid: char[][]) → int Examples Example 1 grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]] return = 1 Every land cell belongs to one four-directionally connected component. Example 2 grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]] return = 3 The grid contains three separate land components. Constraints 1 <= m, n <= 300. m == grid.length and n == grid[i].length. Every cell is either '0' or '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: scan every cell. When you hit an unvisited '1', increment the counter, then flood-fill from that cell so every connected land cell gets marked. The marker is either a separate visited set or you overwrite the cell with '0' in place. Flood-fill can be DFS, BFS, or union-find. All run in O(m*n) time. The common pitfalls are counting diagonals as connected (the problem says only horizontal and vertical), forgetting bounds checks since outside the grid is water, and recursion depth. With a 300 x 300 grid, a single island can have 90,000 cells, so recursive DFS can overflow the stack in some languages. An iterative stack or BFS avoids that. Also confirm you're mutating input only if that's acceptable. If you freeze during the live OA, StealthCoder can supply the traversal while you keep your own narration going.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Number of Islands 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of islands. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Islands FAQ
How hard is Number of Islands really?+
It's a medium on paper, but it's one of the most standard grid problems. If you know flood-fill, you can write it in ten minutes. The difficulty at Google is usually clean code, correct bounds handling, and explaining complexity, not the idea.
What's the trick to solving it?+
Loop over all cells. On each unvisited '1', add one to the count and traverse all connected land, marking it visited. Each cell gets processed once, so you get O(m*n) time. The counter only increments when you start a fresh traversal.
DFS, BFS, or union-find?+
Any works. DFS is shortest to write, BFS avoids deep recursion, and union-find is useful if the interviewer asks a follow-up about dynamic land additions. For a single static grid, pick whichever you can write without bugs under pressure.
Can I modify the input grid?+
Usually yes, setting visited land to '0' saves memory. But state the tradeoff out loud. If the problem or interviewer wants the input preserved, use a separate visited boolean grid, which costs O(m*n) extra space.
How do I prepare in 48 hours?+
Write the DFS and BFS versions from scratch once each, no peeking. Then test on both examples plus a 1x1 grid and an all-water grid. Practice the four-direction array and bounds check until it's automatic. That covers nearly every variant.