Number of Closed Islands
Updated
Problem statement
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[][]) → intExamples
Example 1
grid = [[1,1,1,1,1],[1,0,0,1,1],[1,0,1,0,1],[1,1,1,1,1]]return = 2The 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 = 0The only island reaches the boundary.
Example 3
grid = [[1,1,1],[1,0,1],[1,1,1]]return = 1The center land cell is fully enclosed.
Constraints
1 <= grid.length, grid[i].length <= 100.- The grid is rectangular and contains only
0and1.
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