PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates graph traversal and grid-based pathfinding skills, including connectivity analysis, obstacle handling, and reasoning about time-dependent spreading processes.

  • hard
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Determine escape path with blockers and spreading fire

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given a 2D grid representing a grassland. - Each cell is one of: - `S`: start - `T`: target - `.`: free cell - `#`: blocked cell (cannot enter) - `F`: initial fire source (for Part C) - You can move 4-directionally (up/down/left/right) to adjacent cells. ### Part A: Reachability Given the grid with `#` blockers (no fire yet), determine whether there exists **any path** from `S` to `T`. ### Part B: After adding a blocker Now suppose one additional free cell is turned into a blocker `#` (you are given its coordinates). Determine whether `S` can still reach `T` after this change. ### Part C: Maximum waiting time with spreading fire Now consider fire spread in the grid: - At time `0`, all `F` cells are on fire. - Each minute, fire spreads from burning cells to their 4-directional neighbors that are not blockers `#`. - You start at `S`. You may **wait** at `S` for `w` minutes (where `w ≥ 0` is an integer), then start moving 1 cell per minute. - You cannot enter or stay in a cell that is on fire **at or before** the time you arrive. - Reaching `T` is allowed only if you arrive before it burns (state the rule explicitly in your solution, e.g., “arrive strictly before it burns”). Compute the **maximum** waiting time `w` such that you can still reach `T` safely. If it is impossible even with `w = 0`, return `-1`. If you can wait arbitrarily long (never blocked by fire), return a large sentinel value (e.g., `10^9`). ### Input/Output (for all parts) - Input: `grid` as an `m x n` array of characters; for Part B, also `(r, c)` coordinates to block. - Output: - Part A/B: boolean - Part C: integer maximum `w` ### Constraints (reasonable interview constraints) - `1 ≤ m, n ≤ 300` (or similar) - Grid contains exactly one `S` and one `T`. - Multiple `F` possible (Part C).

Quick Answer: This question evaluates graph traversal and grid-based pathfinding skills, including connectivity analysis, obstacle handling, and reasoning about time-dependent spreading processes.

Escape Path Part C: Maximum Waiting Time with Spreading Fire

You are given an m x n grid of characters. Each cell is one of: `S` (start), `T` (target), `.` (free), `#` (blocked, cannot enter), `F` (initial fire source). You move 4-directionally. Fire model: at time 0 all `F` cells are on fire. Each minute, fire spreads from every burning cell to its 4-directional non-blocker neighbors (`#` cells never burn and also block fire). You start at `S`. You may **wait** at `S` for `w` minutes (integer `w >= 0`), then move 1 cell per minute. You occupy `S` during minutes `0..w` and then occupy each subsequent cell at the minute you step onto it. You may occupy a cell at arrival time `t` only if you arrive **strictly before** it burns, i.e. `t < fire_time[cell]` (a cell on fire at or before your arrival is forbidden, including `T`). Return the **maximum** waiting time `w` for which you can still reach `T` safely. If reaching `T` is impossible even with `w = 0`, return `-1`. If you can wait arbitrarily long (no relevant cell is ever blocked by fire), return the sentinel `10^9`.

Constraints

  • 1 <= m, n <= 300
  • Grid contains exactly one 'S' and one 'T'.
  • Zero or more 'F' (fire source) cells.
  • '#' cells block movement AND block fire spread (never burn).
  • Rule: you may occupy a cell at arrival time t only if t < fire_time[cell] (arrive strictly before it burns).

Examples

Input: ([['S', '.', '.', 'T'], ['.', '.', '.', '.'], ['.', '.', '.', '.'], ['F', '.', '.', '.']],)

Expected Output: 2

Explanation: Fire starts at (3,0). fire_time[S]=3, so w<3. Walking right reaches T at (0,3) in 3 steps; the most constrained path cell forces w<=2, and w=2 is still safe, so the answer is 2.

Input: ([['S', '.', '.', '.', '.', 'T'], ['.', '.', '.', '.', '.', '.'], ['F', '.', '.', '.', '.', '.']],)

Expected Output: 1

Explanation: Fire at (2,0). S=(0,0) catches fire at time 2, so w<2. The constraining path cell limits the max safe wait to w=1.

Input: ([['S', '.', '.', '.', 'T'], ['F', '.', '.', '.', '.']],)

Expected Output: 0

Explanation: Fire at (1,0) reaches S=(0,0) at time 1, so w must be 0. You can still just barely walk to T arriving strictly before it burns, so the answer is 0.

Input: ([['S', '.', '.', 'T', 'F']],)

Expected Output: -1

Explanation: T is adjacent to the fire source, so T burns at time 1. The shortest path to T takes 3 minutes (arriving at time 3), which is not strictly before T burns. Impossible even at w=0, so return -1.

Input: ([['S', '#', 'T']],)

Expected Output: -1

Explanation: A blocker wall separates S from T; T is unreachable regardless of fire, so return -1.

Input: ([['S', '.', 'T']],)

Expected Output: 1000000000

Explanation: There is no fire in the grid, so you can wait arbitrarily long. Return the sentinel 10^9.

Input: ([['S', '.', '.'], ['#', '#', '.'], ['T', '.', '.']],)

Expected Output: 1000000000

Explanation: T is reachable around the '#' wall and there is no fire, so waiting is unbounded; return 10^9.

Hints

  1. First compute, for every cell, the earliest minute it catches fire using a multi-source BFS from all 'F' cells. Blocked cells '#' never burn.
  2. For a fixed wait w, a cell entered at time t is safe iff t < fire_time[cell]; waiting at S for w minutes requires w < fire_time[S]. Run a timed BFS from S where the arrival time at a cell is w + (BFS distance).
  3. Feasibility is monotonic: if you can escape after waiting w, you can also escape waiting any smaller amount (arriving earlier is always at least as safe). So binary-search the largest feasible w.
  4. If a large cap still lets you reach T (or there is no fire at all), waiting is effectively unbounded — return the sentinel 10^9. If you can't reach T even at w=0, return -1.

Escape Path Part A: Reachability from S to T

You are given an m x n grid of characters: `S` (start), `T` (target), `.` (free), `#` (blocked, cannot enter). You may move 4-directionally (up/down/left/right) between adjacent non-blocked cells. Determine whether there exists **any** path from `S` to `T`. Return a boolean.

Constraints

  • 1 <= m, n <= 300
  • Grid contains exactly one 'S' and one 'T'.
  • '#' cells cannot be entered.
  • Movement is 4-directional only.

Examples

Input: ([['S', '.', '.'], ['#', '#', '.'], ['T', '.', '.']],)

Expected Output: True

Explanation: Go right across the top row, down the right column, then left along the bottom row to reach T.

Input: ([['S', '#', 'T']],)

Expected Output: False

Explanation: A blocker wall directly between S and T leaves no path.

Input: ([['S', '.', 'T']],)

Expected Output: True

Explanation: Walk straight right from S to T through the free cell.

Input: ([['S']],)

Expected Output: False

Explanation: Grid has no 'T' cell, so it is unreachable; return False.

Input: ([['S', '.', '.', '.'], ['#', '#', '#', '.'], ['T', '#', '.', '.'], ['.', '#', '.', '#']],)

Expected Output: False

Explanation: T's only open neighbor (3,0) connects solely back to T; the rest of the reachable region is walled off from T, so it is unreachable.

Input: ([['S', '#'], ['#', 'T']],)

Expected Output: False

Explanation: S is fully boxed in by blockers; no move is possible.

Hints

  1. Locate S and T, then run a standard BFS or DFS treating '#' as walls.
  2. Mark cells visited as you enqueue them to avoid revisiting and infinite loops.
  3. Return true as soon as you pop (or reach) the target cell.

Escape Path Part B: Reachability After Adding One Blocker

Same grid as Part A (`S`, `T`, `.`, `#`), but you are additionally given coordinates `(r, c)` of one free cell that is turned into a blocker `#`. Determine whether `S` can still reach `T` after this change. Return a boolean. (If the new blocker lands on `S` or `T`, treat the target as unreachable / blocked.)

Constraints

  • 1 <= m, n <= 300
  • Grid contains exactly one 'S' and one 'T'.
  • 0 <= r < m and 0 <= c < n.
  • If the new blocker lands on S or T, return False.

Examples

Input: ([['S', '.', 'T']], 0, 1)

Expected Output: False

Explanation: The only path from S to T runs through (0,1), which is now blocked, so T is unreachable.

Input: ([['S', '.', '.'], ['.', '.', 'T']], 0, 1)

Expected Output: True

Explanation: Even with (0,1) blocked, you can go down then right: (0,0)->(1,0)->(1,1)->(1,2)=T.

Input: ([['S', '.', '.'], ['#', '.', '.'], ['T', '.', '.']], 1, 1)

Expected Output: True

Explanation: Blocking (1,1) still leaves the route across the top and down the right side, then back to T at (2,0).

Input: ([['S', '.', '.'], ['#', '#', '.'], ['T', '#', '.']], 0, 1)

Expected Output: False

Explanation: With (0,1) blocked, S=(0,0) has no open neighbor ((1,0) is '#'), so it is fully isolated.

Input: ([['S', 'T']], 0, 1)

Expected Output: False

Explanation: The new blocker lands on T itself, so the target is blocked; return False.

Input: ([['S', '.', 'T'], ['.', '.', '.']], 0, 0)

Expected Output: False

Explanation: The new blocker lands on S itself, so you cannot start; return False.

Hints

  1. Reuse the Part A BFS but treat the extra cell (r, c) as if it were a '#'.
  2. Handle the degenerate cases where (r, c) equals the S or T position by returning False up front.
  3. No need to re-mutate the grid — keep (r, c) in a set and skip it during neighbor expansion.
Last updated: Jun 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Determine Whether Courses Can Be Completed - Snapchat (medium)
  • Solve Decimal Coin Change - Snapchat (medium)
  • Find Maximum Island Perimeter - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a Timestamped Counter - Snapchat (medium)