FastPrepCount Enclosed Freshwater Regions
Problems/Count Enclosed Freshwater Regions

Count Enclosed Freshwater Regions

Google logoGoogle● MediumFULLTIMEINTERNPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report links the closed-island freshwater variant. The binary cell convention and four-neighbor rule make the recognizable enclosed-region capability deterministic.

A rectangular map contains land cells marked 1 and freshwater cells marked 0. Freshwater cells belong to the same region when connected horizontally or vertically.

Count freshwater regions that are completely enclosed by land. A region is not enclosed when any of its cells touches the outer boundary of the map.

Function

countEnclosedFreshwaterRegions(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 zeros is one enclosed region and the isolated zero is another.

Example 2

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

The only freshwater region reaches the boundary.

Example 3

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

The center cell is enclosed on every side.

Constraints

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

Source note: Interview prompt excerpt preserved as source evidence.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int countEnclosedFreshwaterRegions(int[][] grid) {
        // Write your solution here.
    }
}
Checking account…