Reported September 2026
Uberdepth first search

Count Connected Islands

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

The grid tops out at 300 by 300, so you've got up to 90,000 cells, and any approach that rescans the grid for every land cell will crawl. That's the whole point of this Uber OA question, reported in September 2026. It's Count Connected Islands, a flood-fill problem in disguise. You walk the matrix once, and each time you hit unvisited land you count one island and sink everything attached to it. If you've seen it, it's five minutes. If you blank under the timer, StealthCoder is the invisible safety net running on the live OA.

The problem

You are given a rectangular binary matrix grid. A cell containing 1 is land and a cell containing 0 is water. Land cells belong to the same island when they are connected horizontally or vertically.
Return the number of islands.

Function
numberOfIslands(grid: int[][]) → 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
All land cells are connected through horizontal or vertical moves.
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 top-left block, center cell, and bottom-right pair are separate islands.
Example 3
grid = [[0,0],[0,0]]
return = 0
No cell is land.

Constraints
1 <= grid.length <= 300.
1 <= grid[i].length <= 300.
Every row has the same length.
Every value is 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one pass plus a flood fill. Loop over every cell. When you see a 1, increment the counter, then run DFS or BFS across the four directions (up, down, left, right, no diagonals) and mark each visited land cell so it never counts again. You can overwrite the cell with 0 to skip a separate visited array. Each cell gets touched a constant number of times, so it's O(rows * cols) time. The common pitfalls are recursion depth and diagonals. A 300 by 300 all-land grid can push recursive DFS to 90,000 frames, which risks a stack overflow in some languages, so use an explicit stack or a BFS queue. Also check bounds before reading a neighbor. If you freeze on the live OA, StealthCoder can hand you the iterative version while you keep typing.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Connected 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of islands. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Connected Islands FAQ

What's the trick to Count Connected Islands?+

Scan every cell once. On an unvisited 1, add one to the count and flood-fill all connected land through the four orthogonal neighbors, marking each cell visited. Every cell is processed a constant number of times, so total work is linear in the grid size.

Should I use DFS or BFS?+

Either works and the answer is identical. With a 300 by 300 grid, recursive DFS can go 90,000 calls deep on an all-land grid, which may overflow the stack. BFS with a queue or DFS with an explicit stack is the safer pick.

Do diagonal cells count as connected?+

No. The problem says land connects horizontally or vertically only. Example 2 confirms it: the center cell and the bottom-right pair are separate islands. Use just four direction offsets, not eight.

Can I modify the input grid?+

The problem doesn't forbid it, and sinking visited land by setting it to 0 is the simplest way to avoid double counting. If you're worried, use a separate boolean matrix of the same size. It costs extra memory but leaves the input untouched.

How do I prepare for this in 48 hours?+

Write the iterative flood fill from scratch twice. Test the all-water grid (answer 0), the single-island grid, and a one-row or one-column grid. Those edge cases catch most bugs. Then practice the same template on a few other grid traversal variants.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it