Reported September 2026
Googleunion find

Count Islands After Land Additions

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

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

This Google OA, reported in September 2026, looks like a grid problem but it's really a connectivity-counting problem in disguise. Land gets added one cell at a time, and after each add you report the island count. It's Union-Find. If you try to rerun a flood fill after every addition, you'll die on 100000 positions over a grid up to 1000 by 1000. If the pattern slips your mind mid-assessment, StealthCoder runs invisibly as a safety net and gives you the solution live.

The problem

Start with a rows by cols grid containing only water. Process each position in positions in order by turning that cell into land.
After every addition, return the number of islands. An island is a maximal group of land cells connected vertically or horizontally. Adding a cell that is already land changes nothing but still produces an output.

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

Examples
Example 1
rows = 3
cols = 3
positions = [[0,0],[0,1],[1,2],[2,1],[1,1]]
return = [1,1,2,3,1]
The final center cell joins the three existing components into one island.
Example 2
rows = 1
cols = 3
positions = [[0,1],[0,1],[0,0],[0,2]]
return = [1,1,1,1]
The repeated addition is a no-op, and both later cells attach to the existing island.
Example 3
rows = 2
cols = 2
positions = [[0,0],[1,1],[0,1],[1,0]]
return = [1,2,1,1]
Diagonal cells are separate. Adding either remaining edge-adjacent cell merges them.

Constraints
1 <= rows, cols <= 1000
1 <= positions.length <= 100000
Every position is [row, col] with 0 <= row < rows and 0 <= col < cols.
Positions may repeat.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: keep a count and a union-find over cells indexed as r*cols+c. When you add a new land cell, increment count by one. Then check its four neighbors. Every neighbor that's already land and sits in a different set triggers a union and decrements count by one. Output count after each step. The pitfall is duplicates. If a position is already land, skip all work and just append the current count, or you'll double count. Another miss is allocating a full 1000x1000 DSU eagerly and re-scanning the grid. Use path compression and union by size or rank so each op is near constant. Total cost is about O(k * alpha(n)) for k positions. Diagonals don't connect, so only check up, down, left, right. StealthCoder is your hedge if the DSU boilerplate or the duplicate edge case escapes you while the clock runs.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Count Islands After Land Additions 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 would have shipped this the night before his JPMorgan OA if he'd had it.

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 Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Islands After Land Additions FAQ

What's the trick to the Google island additions problem?+

Use Union-Find, not repeated DFS. Each new land cell adds one island, then every distinct adjacent land component it merges with subtracts one. Track the running count and append it after each position. That's the whole idea.

How do I handle repeated positions?+

Keep a land marker, like a boolean array or set. If the cell is already land, don't touch the count or unions. Just push the current count to the result. Example 2 tests exactly this, where the repeated [0,1] keeps the answer at 1.

Why not run DFS or BFS after every addition?+

With up to 100000 positions on a grid up to 1000 by 1000, a full scan each time is far too slow. Union-Find updates only the four neighbors of the new cell, so each step is nearly constant time.

Do diagonal cells count as connected?+

No. Only vertical and horizontal neighbors connect. Example 3 shows it: [0,0] and [1,1] stay as two islands until an edge-adjacent cell joins them. Check just four directions when unioning.

How do I prepare for this in 48 hours?+

Write a clean Union-Find with path compression and union by size from memory. Then code this problem once, using index r*cols+c and a land array. Test the three given examples, especially the duplicate and the final merge of three components.

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

OA at Google?
Invisible during screen share
Get it