FastPrepNumber of Closed Islands
Problems/Number of Closed Islands

Number of Closed Islands

Snap Inc. logoSnap Inc.● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Snap Inc. onsite interview. The report describes counting closed islands. We use cells that are 0 or 1, links in four directions, and the rule that an island touching the edge is not closed.

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
grid
01234011111110011210101311111

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
grid
012001110012111

The only island reaches the boundary.

Example 3

grid = [[1,1,1],[1,0,1],[1,1,1]]return = 1
grid
012011111012111

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.

More Snap Inc. interview questions

  • Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
  • Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
  • Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026
  • Sort Characters by FrequencyONSITE INTERVIEW · Seen Sep 2026
  • Dictionary Records to an Editable XML TreeONSITE INTERVIEW · Seen Aug 2026
  • Metric Counter with a Fake ClockONSITE INTERVIEW · Seen Aug 2026
  • ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
  • Basic Calculator IIPHONE SCREEN · Seen Oct 2026
  • Stream Cluster Max and MedianPHONE SCREEN · Seen Oct 2026
  • LRU Cache with Thread-Safety Follow-UpPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Number of IslandsIntuit · ONSITE INTERVIEW
  • Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Shortest Bridge Between Two IslandsCompass International Holdings · ONSITE INTERVIEW
  • Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEW
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEW
  • Foreground Region SizesMicrosoft · ONSITE INTERVIEW
  • The MazeGoldman Sachs · ONSITE INTERVIEW
  • Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEW
  • Number of IslandsTripjack · ONSITE INTERVIEW
  • Number of IslandsTekion · ONSITE INTERVIEW
See Snap Inc. hiring insights
Saved
class Solution {
    public int closedIsland(int[][] grid) {
        // Write your code here.
    }
}
Checking account…