Wait as Long as Possible before Escaping a Spreading Fire
Quick Overview
Find the longest safe delay before escaping a spreading fire. Respect walls, the order of movement and fire spread, and the safehouse arrival exception.
Wait as Long as Possible before Escaping a Spreading Fire
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
### Problem
You are given a rectangular `grid` in which `0` is grass, `1` is fire, and `2` is a wall. You start at the top-left grass cell and want to reach the bottom-right grass cell, the safehouse.
You may first wait at the starting cell for a nonnegative number of minutes. Each minute after you start moving, move one cell north, south, east, or west onto grass; then the fire spreads one cell in those same directions through every non-wall cell. Walls block both you and the fire.
Return the greatest initial waiting time that still lets you reach the safehouse safely. Return `-1` if leaving immediately cannot succeed, or `10^9` if you can wait arbitrarily long. Arriving at the safehouse in the same minute that fire reaches it is allowed; this exception does not apply to other cells.
```cpp
class Solution {
public:
int maximumMinutes(vector<vector<int>>& grid);
};
```
### Examples
**Example 1:**
```text
Input: grid = [[0,1],[0,0]]
Output: -1
Explanation: The safehouse burns at minute 1, while the shortest walk needs two moves.
```
**Example 2:**
```text
Input: grid = [[0,0],[2,0]]
Output: 1000000000
Explanation: No fire exists. The two-step path remains safe after any initial wait.
```
### Constraints
- `2 <= m, n <= 300`, with `4 <= m*n <= 20000`.
- Cells are 0, 1, or 2.
- Both corner endpoints are 0.
### Clarifying Questions
- Does fire spread while you wait at the start? Yes; you must remain safe there.
- Can you enter a wall or an initially burning cell? No.
- Is arrival at the same time as fire allowed anywhere else? No; only the safehouse has that exception.
```hint Check the boundary
Separate the timing rule for an ordinary grass cell from the safehouse rule. Equality is permitted only at the destination.
```
Overview: Find the longest safe delay before escaping a spreading fire. Respect walls, the order of movement and fire spread, and the safehouse arrival exception.
Wait as Long as Possible before Escaping a Spreading Fire
Uber
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0
Problem
You are given a rectangular grid in which 0 is grass, 1 is fire, and 2 is a wall. You start at the top-left grass cell and want to reach the bottom-right grass cell, the safehouse.
You may first wait at the starting cell for a nonnegative number of minutes. Each minute after you start moving, move one cell north, south, east, or west onto grass; then the fire spreads one cell in those same directions through every non-wall cell. Walls block both you and the fire.
Return the greatest initial waiting time that still lets you reach the safehouse safely. Return -1 if leaving immediately cannot succeed, or 10^9 if you can wait arbitrarily long. Arriving at the safehouse in the same minute that fire reaches it is allowed; this exception does not apply to other cells.
class Solution {
public:
int maximumMinutes(vector<vector<int>>& grid);
};
Examples
Example 1:
Input: grid = [[0,1],[0,0]]
Output: -1
Explanation: The safehouse burns at minute 1, while the shortest walk needs two moves.
Example 2:
Input: grid = [[0,0],[2,0]]
Output: 1000000000
Explanation: No fire exists. The two-step path remains safe after any initial wait.
Constraints
2 <= m, n <= 300
, with
4 <= m*n <= 20000
.
Cells are 0, 1, or 2.
Both corner endpoints are 0.
Clarifying Questions Guidance
Does fire spread while you wait at the start? Yes; you must remain safe there.
Can you enter a wall or an initially burning cell? No.
Is arrival at the same time as fire allowed anywhere else? No; only the safehouse has that exception.