Escape a Million by Million Grid Past a Few Blocked Cells

Last updated: November 29, 2026

Quick Overview

On a 10^6 by 10^6 grid, up to 200 cells are blocked. Given a source and target, return True if you can walk from one to the other using 4-directional moves through unblocked cells.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


On a 10^6 by 10^6 grid, up to 200 cells are blocked. Given a source and target, return True if you can walk from one to the other using 4-directional moves through unblocked cells.

A Google onsite question that tests reasoning about bounds instead of brute force, since the grid has $10^{12}$ cells. The interviewer wants the insight that a small number of blocked cells can only enclose a bounded area, so a capped BFS from each side decides the answer.

What the Interviewer Expects
  • Recognize full BFS over 10^12 cells is impossible
  • Bound the area that B blocked cells can enclose (about B^2 / 2)
  • Run a capped BFS from source and from target
  • Declare escape when the BFS exceeds the cap
  • Explain why both directions must be checked
Key Topics to Cover
bounded BFS
geometry argument
hash set
implicit huge grid
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
  • Prove the area bound for B blocked cells against the grid corner.
  • What if blocked cells number in the millions?
  • Return the shortest path length when it exists.
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
  • Grid coordinates in [0, 10^6).
  • blocked has up to 200 cells; source and target are not blocked.
  • Output: whether target is reachable from source....
High-Level Design

With BB blocked cells, the largest region they can seal off is when they form a diagonal across a corner of the grid, enclosing about B(B−1)/2B(B-1)/2 cells (19,900 for B=200B = 200). So if a BFS from the sou...


Submit Your Answer
Markdown supported