Number of Islands
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in February 2023, and the grid can hit 300 by 300. That's 90,000 cells, so any approach that rescans the grid for every land cell or re-counts the same blobs will burn time you don't have. It's Number of Islands: count the connected groups of '1' cells, linked horizontally or vertically. The input comes as an array of strings, which is the only twist. If you've got an OA invite, this is a flood-fill problem and you can write it in ten minutes. StealthCoder sits invisible on your screen as a safety net if your mind goes blank mid-assessment.
The problem
Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands. An island is a maximal group of land cells connected horizontally or vertically. Cells outside the matrix are water. Function numIslands(grid: String[]) → int Examples Example 1 grid = ["11110","11010","11000","00000"] return = 1 All land cells belong to one orthogonally connected component. Example 2 grid = ["11000","11000","00100","00011"] return = 3 The upper-left block, center cell, and lower-right pair are separate islands. Example 3 grid = ["000","000"] return = 0 The grid contains no land cells. Constraints 1 <= grid.length <= 300. 1 <= grid[i].length <= 300, and every row has the same length. Every character is '0' or '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to visit each cell once. Scan every cell. When you hit an unvisited '1', increment the count, then flood-fill the whole island with DFS or BFS so none of its cells get counted again. Four directions only, no diagonals. The common pitfall is the input type. Strings are immutable in most languages, so you can't write '0' into the grid to mark cells visited. Use a separate boolean matrix, or convert rows to character arrays first. The second pitfall is recursion depth. A 300 by 300 grid of all land can recurse 90,000 levels deep and overflow the stack in some languages, so use an explicit stack or BFS queue if you're worried. Time is O(rows times cols). If you blank on the visited-marking detail during the live OA, StealthCoder is the hedge that gives you a working solution fast.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
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 Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Islands FAQ
What's the trick for Number of Islands?+
Scan every cell. Each time you find unvisited land, add one to the count and flood-fill the entire connected island with DFS or BFS so it's never counted twice. Every cell is processed once, so it runs in O(rows times cols). That's all there is to it.
How hard is this really for the Bloomberg OA?+
It's a medium and one of the most common grid problems. Bloomberg reported it in February 2023. If you know flood fill, the logic is short. The risk is small bugs like bounds checks, visited marking, or treating diagonals as connected.
Why do the grid rows come as strings?+
The input is an array of strings, and strings are immutable in most languages. You can't flip '1' to '0' in place. Either keep a separate visited array of booleans or copy each row into a character array before you start.
DFS or BFS, which should I use?+
Either works with the same complexity. DFS recursion is shorter to write but can go 90,000 frames deep on a full 300 by 300 grid of land. BFS with a queue or DFS with an explicit stack avoids stack overflow risk and is the safer pick.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice without looking. Test it on the all-water grid, a single-cell grid, and a full-land grid. Then try the follow-up of using union-find instead. Flood fill on a matrix shows up often, so the pattern carries over to other problems.