Infection Spread Time on a Grid
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to apply multi-source breadth-first search to simulate simultaneous state propagation across a 2D grid. It tests graph traversal, reachability reasoning, and time-complexity awareness — core competencies commonly assessed in coding interviews for software engineering roles. The follow-up variant on 8-directional spread further probes adaptability and spatial reasoning within the same algorithmic framework.
Infection Spread Time on a Grid (4-Directional)
Constraints
- 1 <= m, n <= 300
- grid[i][j] is one of 0, 1, or 2
- There may be zero, one, or many initially infected cells
- Empty cells (0) block infection and are never counted as healthy
- An O(m * n) solution is expected
Examples
Input: ([[2,1,1],[1,1,0],[0,1,1]],)
Expected Output: 4
Explanation: Single infected cell at top-left; the bottom-right healthy cell is last, infected at minute 4.
Input: ([[2,1,1],[0,1,1],[1,0,1]],)
Expected Output: -1
Explanation: The healthy cell at (2,0) is walled off by 0s and never gets infected, so return -1.
Input: ([[0,2]],)
Expected Output: 0
Explanation: No healthy cells to infect, so 0 minutes.
Input: ([[1]],)
Expected Output: -1
Explanation: A lone healthy cell with no infected source can never be infected.
Input: ([[2]],)
Expected Output: 0
Explanation: Already fully infected, no healthy cells remain.
Input: ([[0,0],[0,0]],)
Expected Output: 0
Explanation: Only empty cells; nothing to infect.
Input: ([[2,1,1,1,1,1,1,2]],)
Expected Output: 3
Explanation: Two infected ends close in on the middle simultaneously; the meeting cells are reached at minute 3.
Input: ([[2,2],[1,1],[0,1],[1,1]],)
Expected Output: 4
Explanation: Spread goes down column then around the 0 wall; the cell at (3,0) is last, at minute 4.
Hints
- Use multi-source BFS: seed the queue with EVERY initially infected cell at once, not one at a time.
- Process the queue one BFS layer per minute. Counting layers (not total nodes) gives you the elapsed minutes.
- Track the number of remaining healthy cells. If it's already 0, the answer is 0; if it's still > 0 after the queue drains, return -1.
- Empty cells (0) are walls — never spread into them and never count them as healthy.
Infection Spread Time on a Grid (8-Directional Follow-up)
Constraints
- 1 <= m, n <= 300
- grid[i][j] is one of 0, 1, or 2
- Infection spreads to all 8 neighbors (4 orthogonal + 4 diagonal)
- Empty cells (0) block infection and are never counted as healthy
- An O(m * n) solution is expected
Examples
Input: ([[2,1,1],[1,1,0],[0,1,1]],)
Expected Output: 2
Explanation: With diagonals, (0,0) reaches (1,1) at minute 1, and (1,1) reaches the bottom-right via a diagonal at minute 2 — faster than the 4-directional answer of 4.
Input: ([[2,1,1],[0,1,1],[1,0,1]],)
Expected Output: 2
Explanation: Diagonal moves let infection reach (2,0) and (2,2) (walled off in the 4-dir version), finishing at minute 2.
Input: ([[0,2]],)
Expected Output: 0
Explanation: No healthy cells.
Input: ([[1]],)
Expected Output: -1
Explanation: Lone healthy cell, no source.
Input: ([[2]],)
Expected Output: 0
Explanation: No healthy cells.
Input: ([[0,0],[0,0]],)
Expected Output: 0
Explanation: Only empty cells.
Input: ([[2,0,0,0,1]],)
Expected Output: -1
Explanation: The healthy cell is separated by a run of 0s with no diagonal bypass on a single row, so it's unreachable.
Input: ([[2,1,1],[1,1,1],[1,1,1]],)
Expected Output: 2
Explanation: From the corner, minute 1 covers the 3 nearest cells (including diagonal (1,1)), minute 2 covers the rest.
Input: ([[1,1,1],[1,2,1],[1,1,1]],)
Expected Output: 1
Explanation: The center infects all 8 surrounding cells in a single minute.
Hints
- The algorithm is unchanged from the 4-directional version — only the direction list grows from 4 offsets to all 8.
- Diagonal offsets are (-1,-1), (-1,1), (1,-1), (1,1); add these to the orthogonal four.
- Because diagonals let infection slip past some 0 walls, the same grid can finish faster than under 4-directional spread.
- A healthy cell still returns -1 if NO infected cell can reach it via any 8-directional path through non-empty cells.