Number of Islands in Three Dimensions
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Two cells stacked on the z axis count as one island, but the diagonal neighbor in the same layer doesn't. That's the detail Bloomberg's April 2021 OA hangs on. It's Number of Islands with a depth axis added, so you're flood-filling a 3D grid through six face neighbors. The grid can hold up to 10^6 cells, which matters for how you code it. If the pattern clicks, this is a quick one. If you blank on the traversal, StealthCoder is the safety net running invisibly during the live OA, so you aren't stuck staring at an empty editor.
The problem
grid[z][r][c] is 1 for land and 0 for water. Land cells belong to the same island when connected through shared faces along depth, row, or column axes. Return the number of islands. Function numIslands3D(grid: int[][][]) → int Examples Example 1 grid = [[[1,0],[0,1]],[[1,0],[0,0]]] return = 2 The two cells at [0,0,0] and [1,0,0] connect by a face; the diagonal cell is separate. Constraints The rectangular 3D grid contains at most 10^6 cells.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a flood fill. Scan every cell in z, r, c order. When you hit an unvisited 1, increment the island count and spread through all connected land, marking cells as you go. Neighbors are only the six face directions: z plus or minus 1, r plus or minus 1, c plus or minus 1. No diagonals, which the example calls out. The pitfall is recursion. With up to 10^6 cells, one long snaking island can blow the call stack in many languages, so use an explicit stack or a BFS queue. Mark cells visited when you push them, not when you pop them, or you'll enqueue duplicates and waste time. Mutating the input to 0 is fine unless the problem forbids it. Check bounds on all three axes before indexing. If the iterative version slips your mind mid-assessment, StealthCoder can hand you the clean version in real time. Complexity is O(cells) time and space.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Number of Islands in Three Dimensions 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
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg 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 in Three Dimensions FAQ
What's the trick in Bloomberg's 3D Number of Islands?+
Treat it as the classic 2D islands problem with a third axis. Loop over every cell, and when you find unvisited land, count one island and flood-fill all connected land using the six face-adjacent directions. Diagonals never connect, as the example shows.
Should I use DFS or BFS for this?+
Either works, but with up to 10^6 cells, recursive DFS risks a stack overflow on a large single island. Use BFS with a queue or DFS with an explicit stack. Both run in linear time over the number of cells.
How hard is this really?+
It's a medium if you already know 2D Number of Islands. The only new parts are the extra dimension and the six direction offsets. If you've never done the 2D version, expect to spend more time on the traversal and bounds checks.
What mistakes cost people the most here?+
Counting diagonals as connected, forgetting a bounds check on the depth axis, and marking visited too late so cells get queued twice. Also watch the index order. The grid is grid[z][r][c], so mixing up dimensions causes silent wrong answers.
How do I prepare in 48 hours?+
Write the 2D island flood fill from memory using an iterative stack. Then extend it to 3D by swapping the four directions for six. Test on the example, an all-water grid, and an all-land grid. That covers the edge cases this problem is likely to hit.