Count Enclosed Freshwater Regions
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip the water story away and this Google OA, reported in September 2026, is a connected-components count with one filter: ignore any component that touches the edge. That's the whole problem. It's the same shape as Number of Closed Islands, just with the labels flipped. If you've got an invite and 48 hours, you only need one flood fill and one rule about borders. The grid goes up to 500 by 500, so the implementation details matter more than the idea. If you blank on the day, StealthCoder runs invisibly on screen as a safety net and hands you the approach while you keep typing.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to remove the boundary problem before you count anything. Run DFS or BFS from every 0 on the outer border and mark everything it reaches as visited, or flip it to 1. Then scan the interior, and every time you hit an unvisited 0, increment the count and flood fill it. Each cell gets touched a constant number of times, so it's O(rows * cols) time. The common pitfall is recursive DFS on a 500 by 500 grid. That's 250,000 cells and can blow the stack in some languages, so use an explicit stack or a queue. Other misses: counting diagonal neighbors (only four directions count), and checking the border per region instead of up front. A single-row or single-column grid has no enclosed cells, and the border-first approach handles that automatically. If the live OA freezes your brain, StealthCoder is the hedge for the flood fill boilerplate.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Enclosed Freshwater Regions 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of closed islands. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Enclosed Freshwater Regions FAQ
What's the trick in Count Enclosed Freshwater Regions?+
Flood fill from every border 0 first and mark those cells as visited. After that, any 0 you still find belongs to an enclosed region. Count each new unvisited 0 as one region, flood fill it, and move on. No per-region boundary flag needed.
Should I use DFS or BFS here?+
Either works. With grids up to 500 by 500, recursive DFS risks a stack overflow in some languages. BFS with a queue, or DFS with an explicit stack, is safer. Time is O(rows * cols) and extra space is the same in the worst case.
Do diagonal cells connect water regions?+
No. The problem says regions connect horizontally or vertically only. Use four directions: up, down, left, right. Adding diagonals is the most common way to get Example 1 wrong, because it would merge regions that should stay separate.
Is this similar to a known LeetCode problem?+
Yes. It's very close to Number of Closed Islands, where 0 is land and 1 is water. Here the roles are swapped, so 0 is the freshwater and 1 is the land that encloses it. If you've solved that one, you've already solved this.
How do I prepare for this in 48 hours?+
Write the border-first flood fill from scratch twice, on a blank editor. Test the three given examples plus a 1 by N grid and an all-zero grid. Those edge cases should return 0. Also practice an iterative traversal so you're not caught by recursion depth.