Find the First Step When an Opening Grid Percolates

Last updated: November 29, 2026

Quick Overview

An n by n grid starts fully blocked. Cells open one at a time in a given order. Return the 1-based step at which some path of open cells first connects the top row to the bottom row, or -1 if it never does.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


An n by n grid starts fully blocked. Cells open one at a time in a given order. Return the 1-based step at which some path of open cells first connects the top row to the bottom row, or -1 if it never does.

Percolation is a textbook union-find application, and Google screens use it to see if you know the virtual node trick. Framed as water seeping through soil layers for a Maps flood model, the interviewer checks that you add top and bottom sentinels so the connectivity test is a single comparison.

What the Interviewer Expects
  • Introduces virtual TOP and BOTTOM nodes
  • Connects first-row cells to TOP and last-row cells to BOTTOM on opening
  • Unions each new cell with open neighbors
  • Checks find(TOP) == find(BOTTOM) after each step
  • Avoids re-scanning the top row on every check
Key Topics to Cover
Union-Find
Virtual Nodes
Grid
Percolation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • The backwash problem: with one BOTTOM node, can you correctly say whether a specific cell is connected to the top?
  • Estimate the percolation threshold with Monte Carlo trials.
  • What if water can also flow diagonally?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n and a list of (r, c) openings.
  • Paths use four-directional adjacency.
  • Output: first step (1-based) at which top and bottom connect, else -1....
High-Level Design

Without sentinels, checking percolation means testing every top cell against every bottom cell. Instead add two extra nodes. Opening a cell in row 0 unions it with TOP; opening a cell in the last row ...


Submit Your Answer
Markdown supported