Reported October 2026
Snapdepth first search

Number of Closed Islands

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

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

The mistake that sinks a first attempt on this Snap OA, reported in October 2026, is counting every island and then trying to subtract the bad ones. Closed Island looks like a plain flood fill problem, and it is, but the order of operations decides whether you pass. Land is 0, water is 1, which flips what most people expect. If you've got an assessment coming, learn the one-line fix below. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is learnable tonight.

The problem

A rectangular grid contains land cells marked 0 and water cells marked 1. Land cells belong to the same island when connected horizontally or vertically.
Return the number of islands made entirely of land cells that do not touch the grid boundary.

Function
closedIsland(grid: int[][]) → int

Examples
Example 1
grid = [[1,1,1,1,1],[1,0,0,1,1],[1,0,1,0,1],[1,1,1,1,1]]
return = 2
The left group of three land cells and the isolated land cell are both closed.
Example 2
grid = [[0,1,1],[0,0,1],[1,1,1]]
return = 0
The only island reaches the boundary.
Example 3
grid = [[1,1,1],[1,0,1],[1,1,1]]
return = 1
The center land cell is fully enclosed.

Constraints
1 <= grid.length, grid[i].length <= 100.
The grid is rectangular and contains only 0 and 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to kill the boundary islands first. Run a DFS or BFS from every 0 on the edge of the grid and flip all connected land to 1. After that, any 0 left over belongs to a closed island. Then scan the grid once more, and each time you hit a 0, increment the count and flood it to 1 so you don't recount it. The common pitfall is counting during a single pass and checking a boundary flag per island. That works but it's easy to botch when the DFS returns early and leaves half an island unvisited. Another trap is treating 1 as land out of habit. Grid is up to 100 by 100, so recursion depth can reach 10,000. Use an iterative stack if your language is touchy. If you freeze in the live OA, StealthCoder is the hedge that hands you the structure fast. Time is O(rows times cols).

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 Number of Closed Islands 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 closed islands. If you have time before the OA, drill that.

⏵ The honest play

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

Snap 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.

Number of Closed Islands FAQ

What's the trick to Number of Closed Islands?+

Remove boundary-touching land first. Flood fill from every 0 on the edges and turn it into 1. Whatever 0 cells remain are enclosed. Then count connected components of 0 with a second flood fill pass. Two simple passes beat tracking flags inside one DFS.

How hard is this problem really?+

Medium on paper, easy if you've done flood fill before. The logic is short. Most failures come from flipped land and water values or from counting islands that touch the border. Get the boundary step right and the rest is routine grid traversal.

Should I use DFS or BFS?+

Either works. DFS is shorter to write recursively, but a 100 by 100 grid can push recursion depth to 10,000 in a worst case. If your language has a low stack limit, use BFS with a queue or an iterative DFS with an explicit stack.

Is the island pattern still asked at Snap?+

This Snap OA was reported in October 2026, so grid flood fill is clearly still in rotation. Expect variants like counting enclaves, max area of island, or surrounded regions. The same boundary-first idea carries over to all of them.

How do I prepare in 48 hours?+

Write this solution from scratch twice without looking. Then do one variant, like enclaves or max area. Practice the four-direction neighbor loop and bounds checks until they're automatic. That covers most grid traversal questions you'll see in an OA.

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

OA at Snap?
Invisible during screen share
Get it