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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Grid coordinates in
[0, 10^6). blockedhas up to 200 cells; source and target are not blocked.- Output: whether target is reachable from source....
High-Level Design
With blocked cells, the largest region they can seal off is when they form a diagonal across a corner of the grid, enclosing about cells (19,900 for ). So if a BFS from the sou...