The Find a Safe Walk Through a Grid interview question involves navigating a 2D grid where some cells contain "hazards" or obstacles that reduce your "health" or "safety" points. You start at the top-left corner with a specific amount of initial health and want to reach the bottom-right corner. Each time you step into a cell with a hazard, your health decreases. You need to determine if there is a path where you can reach the destination with at least 1 health point remaining.
Google and Bloomberg use this to test a candidate's proficiency with graph traversal and optimization. It's essentially a shortest-path problem where the "distance" is the health cost. It evaluations your ability to use the Breadth-First Search interview pattern or Dijkstra's algorithm to find the path that minimizes damage taken. It also checks how you handle state management within a grid.
This problem is best solved using Dijkstra's Algorithm or 0-1 BFS. Since you want to maximize your remaining health, you are essentially looking for the "shortest path" in terms of health cost. You can maintain a max_health_at[r][c] 2D array to store the maximum health you can have when reaching each cell. A Heap (Priority Queue) can be used to always explore the cell where you currently have the highest health.
Grid ():
[0, 1]
[0, 0]
Initial Health: 2.
When you see a grid problem involving "costs," "health," or "weights," immediately think of Dijkstra's algorithm. If the costs are only 0 and 1, you can use a Deque (0-1 BFS) to achieve time complexity, which is faster than a Priority Queue.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Obstacle Removal to Reach Corner | Hard | Solve | |
| Minimum Time to Visit a Cell In a Grid | Hard | Solve | |
| Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Solve | |
| The Maze II | Medium | Solve | |
| Find Minimum Time to Reach Last Room I | Medium | Solve |