Reported September 2026
Uberunion find

Dynamic Island Counts

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 Uber OA reported in September 2026 hands you a grid up to 10^7 cells and 100000 land additions, and asks for the island count after each one. Rerunning BFS after every addition is dead on arrival. That's the whole point of the question. The hinted pattern says breadth-first search, but the real answer is union-find, which treats each added cell as a merge instead of a rescan. If the grid size makes your brain freeze, StealthCoder is the invisible safety net on the live OA. Know the trick first, though, and you probably won't need it.

The problem

An initially all-water grid has rows rows and cols columns. Process the positions in order. For each [row, col], turn that cell into land if it is still water, then record the current number of horizontally or vertically connected islands.
If a position is repeated, the grid does not change and the previous island count is recorded again. Return one count per position.

Function
dynamicIslandCounts(rows: int, cols: int, positions: int[][]) → int[]

Examples
Example 1
rows = 3
cols = 3
positions = [[0,0],[0,1],[1,2],[2,1]]
return = [1,1,2,3]
The second addition joins the first island; the final two additions are isolated from existing land.
Example 2
rows = 3
cols = 3
positions = [[0,0],[0,1],[1,2],[1,1]]
return = [1,1,2,1]
The last cell bridges the two existing islands into one.
Example 3
rows = 1
cols = 2
positions = [[0,0],[0,0],[0,1]]
return = [1,1,1]
The repeated position is a no-op, and the final cell joins the existing island.

Constraints
1 <= rows, cols <= 10000.
1 <= positions.length <= 100000.
Every position contains exactly two integers within the grid.
rows * cols <= 10^7.

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is the classic Number of Islands II setup. Keep a union-find over flattened indices (row * cols + col). For each position, if the cell is already land, append the previous count and move on. Otherwise mark it land, increment the count by one, then check its four neighbors. Every neighbor that is land and in a different set gets unioned, and each successful union decrements the count. Use path compression and union by size so each operation is near constant. The pitfall is brute force: a BFS per addition costs up to 10^7 work times 100000 queries. The second pitfall is skipping the duplicate check, which inflates the count. Allocate the parent array lazily or as one flat array sized rows * cols, which fits under 10^7. If you blank on the merge logic during the live OA, StealthCoder can surface the union-find structure and you can adapt it quickly.

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 Dynamic Island Counts 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 ii. 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.

Dynamic Island Counts FAQ

What's the trick for Dynamic Island Counts?+

Use union-find instead of BFS. Each new land cell adds one island, then every distinct neighboring island you merge with subtracts one. You never rescan the grid, so each query costs almost constant time.

Why does BFS fail here even though it's the hinted pattern?+

BFS or DFS per addition touches up to 10^7 cells, and you have up to 100000 additions. That's far too slow. BFS is fine for a static grid, but this problem is online and needs incremental merging.

How do I handle repeated positions?+

Check whether the cell is already land before doing anything. If it is, push the previous count onto the result and skip the union logic. Example 3 tests exactly this, with [0,0] repeated and the count staying at 1.

Should I use a 2D array or a hash map for the parent structure?+

Since rows * cols is at most 10^7, a flat integer array indexed by row * cols + col works and is faster than a hash map. A map is fine if you want lazy allocation, but the flat array is simpler and quicker.

How do I prepare for this in 48 hours?+

Write union-find from memory with path compression and union by size. Then code this problem once, testing the three examples. Focus on the count bookkeeping: plus one per new cell, minus one per successful merge. That's the part people fumble.

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