Reported October 2026
Amazonunion find

Count Islands from Streaming Rows

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

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

The whole problem hinges on one data structure: union-find. This Amazon OA, reported in October 2026, hands you a binary matrix one row at a time and wants the island count after every row, not just at the end. If you're taking it in the next day or two, don't reach for a fresh BFS on each row. That's the trap. Components merge as rows land, and a disjoint set tracks that cheaply. StealthCoder is the safety net if your mind goes blank mid-assessment, but you can walk in knowing the shape of the answer.

The problem

A binary matrix arrives one row at a time from top to bottom. Given those rows as the finite ordered batch rows, return an array where result i is the number of islands present after rows 0 through i have arrived.

Examples
Example 1
rows = ["110","010","011"]
return = [1,1,1]
Each new land cell joins the component that began in the first row.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Treat each land cell as a node in a union-find. Keep a running island count. When a new row arrives, every land cell in it starts as its own component, so add one to the count. Then check its left neighbor in the same row and the cell directly above it in the previous row. Each successful union of two different roots drops the count by one. Append the count to the result after finishing each row. Only the previous row's cell ids matter for linking, so you can index cells as row * width + col. The common pitfall is rerunning DFS or BFS over the whole grid after every row, which turns a near-linear job into something quadratic. Another miss is forgetting that one new cell can bridge two existing islands, so the count can go down. In the live OA, StealthCoder is your hedge if the union-find setup slips your mind under pressure.

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 from Streaming Rows 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

⏵ The honest play

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

Amazon 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 from Streaming Rows FAQ

What's the trick in Count Islands from Streaming Rows?+

Use union-find with a running island count. Each new land cell adds one, and every union that merges two distinct roots subtracts one. Only the left neighbor and the cell above can connect, since rows arrive top to bottom. Record the count after each row.

How hard is this Amazon OA question really?+

Medium. If you know union-find, it's a small variation on the classic islands problem. The difficulty is spotting that you shouldn't recompute from scratch per row. Without that insight, you'll write something correct but too slow.

Can the island count go down as rows arrive?+

Yes. A new land cell can connect two islands that were separate before. Example: land on the left and right of a row both reach down to a new row's land cell. The merge reduces the count, so you must track unions, not just additions.

Why not just run DFS after every row?+

It works but repeats the full scan each time, so cost grows with rows times total cells. With large inputs that likely times out. Union-find updates incrementally and only touches the new row's cells and their two relevant neighbors.

How do I prepare for this in 48 hours?+

Write a union-find with path compression and union by size from memory. Then solve the standard islands problem with it, and extend it to add cells one at a time. Test with a case where a new cell merges two islands and the count drops.

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

OA at Amazon?
Invisible during screen share
Get it