HARDasked at 5 companies

Number of Islands II

A hard-tier problem at 40% community acceptance, tagged with Array, Hash Table, Union Find. Reported in interviews at Snap and 4 others.

Founder's read

Number of Islands II is the harder cousin of the classic island-counting problem, and it's asked at Snap, Dropbox, Google, and Uber. Instead of a static grid, you're adding land cells one at a time and tracking how many islands exist after each addition. The acceptance rate is 40%, which tells you most candidates either don't recognize the Union Find pattern or implement it wrong under pressure. If this hits your live assessment and you freeze on the state-management trick, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
5
Difficulty
HARD
Acceptance
40%

Companies that ask "Number of Islands II"

If this hits your live OA

Number of Islands II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.

Get StealthCoder
What this means

The trap is treating this like a standard grid DFS problem. That fails because the grid doesn't exist upfront; you're building it incrementally. The real trick is Union Find. Each land cell is initially its own island. When you add a new cell, you union it with any adjacent land cells already present, decrementing the island count each time a union happens. Hash Table tracks which cells exist. Array stores parent pointers for the Union Find structure. Most candidates either skip Union Find entirely and time out, or implement a buggy union operation that double-counts merges. StealthCoder handles the parent-tracking and union logic so you don't have to debug it live.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Islands II recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Islands II interview FAQ

Why can't I just run DFS on the grid after each addition?+

DFS works for the static version, but here you're adding cells one at a time and answering a query after each step. Re-running DFS from scratch is O(n) per addition, making the total O(n^2) or worse. Union Find does the same thing in nearly O(1) amortized time per addition, which is why the problem exists.

Is this really asked at these companies or just theoretical?+

It appears in reports from Snap, Dropbox, Google, and Uber. It's not a warmup. These are companies that care about graph algorithms and dynamic data structures. If you're interviewing there, assume it's in the pool.

What's the trick to not mess up the union logic?+

Track island count explicitly. Start each new cell as its own island (count + 1). For each adjacent cell that exists, union once and decrement count. The count never increases; it only stays the same or decreases. If your count goes up, you're adding cells wrong.

Do I need to handle the grid boundaries or is it given?+

The problem gives you a list of land cell coordinates, not a full grid. Hash Table to check neighbor existence. No need to allocate a 2D array. That's the optimization most people miss.

How does this relate to the other Union Find problems I've drilled?+

It's Union Find plus state management. The core find and union operations are standard. The difference is tracking the island count as a side effect of unions, not just asking 'are two cells connected.' That shift trips up candidates who only memorized template code.

Want the actual problem statement? View "Number of Islands II" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.