Number of Closed Islands
A medium-tier problem at 67% community acceptance, tagged with Array, Depth-First Search, Breadth-First Search. Reported in interviews at DoorDash and 0 others.
Number of Closed Islands is a medium-difficulty matrix traversal problem that tests your ability to identify and count bounded regions in a 2D grid. You're given a matrix of 0s and 1s, and you need to count how many islands are completely surrounded by 1s (can't reach the boundary without crossing a 1). DoorDash has asked this in their online assessments. The 67% acceptance rate tells you it's not a gimme: the boundary condition trips up candidates who code a standard island-counting solution without thinking about what "closed" actually means. If this pattern hits your live OA and you blank on the closure check, StealthCoder solves it invisibly during your screen share.
Companies that ask "Number of Closed Islands"
Number of Closed Islands 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 a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe trap here is treating this like a vanilla island-count problem using DFS or BFS. The trick is that an island is only "closed" if it never touches the grid boundary. Most candidates flood-fill from each unvisited 0, but they forget to track whether their traversal went out of bounds. The right approach: mark a region as invalid the moment your search hits row 0, row n-1, column 0, or column m-1. Only count regions that stay fully interior. Union Find works too if you union all boundary-touching cells into a single "exterior" component first, then count interior components. The pattern generalizes: any flood-fill problem where you care about containment requires a boundary-awareness check. StealthCoder delivers the closure-validation logic instantly, so even if the pattern is unfamiliar during your assessment, you have a working solution in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Number of Closed Islands recycles across companies for a reason. It's medium-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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Closed Islands interview FAQ
Is this just counting islands with an extra check?+
Not quite. Standard island-counting doesn't care about boundaries. Here, you must detect if your DFS or BFS ever tries to cross into an out-of-bounds cell or touch the perimeter. If it does, that region is open, not closed. Same traversal skeleton, different termination logic.
Should I use DFS, BFS, or Union Find?+
DFS is fastest to code and most memory-efficient for this problem. BFS works identically. Union Find is overkill unless the problem adds edge cases like multiple queries or island merging. Stick with DFS for an OA.
How do I check if an island is closed during traversal?+
Flag the region as open if your search ever tries to access grid[r][c] where r or c is out of bounds, or if r/c equals 0, n-1, or m-1. Only count regions where this flag stays false after full traversal.
Does DoorDash ever ask the closed-islands variant?+
Yes, DoorDash has this on record. It's a logistics-flavored problem: finding warehouses or service zones that don't leak to the boundary. Medium difficulty, but the boundary logic is what separates quick passes from timeouts.
What if the grid is all 0s or all 1s?+
All 0s means the entire grid is one open island. All 1s means zero islands. Edge cases are real in OAs. Handle them before your main loop by returning 0 or checking grid dimensions.
Want the actual problem statement? View "Number of Closed Islands" on LeetCode →