Number of Islands II
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported Number of Islands II in July 2026, and the whole question hinges on one data structure: union-find, also called DSU. Land appears one cell at a time and you report the island count after every addition. If you try to re-scan the grid each time, you're dead. The interviewer follow-up reportedly went straight into how DSU works, edge cases, and complexity, so you need to be able to explain it, not just type it. If you blank on the live OA, StealthCoder is the safety net that reads the problem and hands you the structure while the proctor sees nothing.
The problem
Start with an m by n grid containing only water. For each distinct position [row, col] in positions, turn that cell into land and append the current number of islands to the result. An island is a maximal group of land cells connected vertically or horizontally. Diagonal cells are not connected. Interview follow-up The rest of the discussion covered the DSU approach, how union-find works, edge cases, and the time and space complexity. Function numIslands2(m: int, n: int, positions: int[][]) → int[] Examples Example 1 m = 3 n = 3 positions = [[0,0],[0,1],[1,2],[2,1]] return = [1,1,2,3] The second cell joins the first island. The final two additions are not four-directionally adjacent to existing land. Example 2 m = 2 n = 2 positions = [[0,0],[1,1],[0,1]] return = [1,2,1] The third addition connects the two existing islands into one.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is incremental connectivity. Keep a parent array (or map) over cells indexed as row * n + col. When you add a land cell, bump the count by one, then check its four neighbors. For each neighbor that's already land, run find on both cells, and if the roots differ, union them and decrement the count. Append the count after each position. Use path compression and union by rank or size so each operation is near constant, giving roughly O(k * alpha(mn)) for k positions. The classic pitfalls are duplicate positions (a cell already land must not increment the count again), treating diagonals as connected, and forgetting bounds checks. Example 2 is the one to trace: the third addition merges two islands and drops the count from 2 to 1. StealthCoder is your hedge in the live OA if the union logic slips under pressure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Number of Islands II 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of islands ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Islands II FAQ
What's the trick in Number of Islands II?+
Use union-find instead of re-running a flood fill. Each new land cell adds one island, then every land neighbor that sits in a different set merges with it and subtracts one. That makes each addition nearly constant time instead of a full grid scan.
How hard is this problem really?+
It's a hard-tagged problem, but it's mostly one idea. If you already know DSU with path compression, it's about 30 lines. The difficulty is recognizing that the grid only grows, so connectivity can be maintained incrementally.
What edge cases should I handle?+
Duplicate positions are the big one. If a cell is already land, append the current count and skip the add. Also check bounds on all four neighbors, ignore diagonals, and handle an empty positions list by returning an empty array.
What complexity should I state?+
With path compression and union by size or rank, each operation is amortized near O(alpha(mn)), so total time is about O(k) for k positions, plus up to O(mn) space for the parent structure. Say that the grid is never rescanned.
How do I prepare in 48 hours?+
Write a DSU class from memory with find, union, and a count. Then trace both examples by hand, especially the merge in example 2. Be ready to explain why path compression matters, since the Amazon follow-up in July 2026 focused on how union-find works.