Simulate Plant Infection Spread
Company: OpenAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates graph traversal and grid-based simulation skills, including modeling a 2D grid as a graph, propagation dynamics with obstacles, multi-source propagation, and analysis of time and space complexity.
Part 1: Minimum Minutes to Infect All Plants
Constraints
- 0 <= m, n <= 200
- Each cell is one of: 0 (empty), 1 (healthy), 2 (infected), 3 (obstacle)
- Infection spreads only up, down, left, and right
Examples
Input: ([[2, 1, 1], [1, 1, 0], [0, 1, 1]],)
Expected Output: 4
Explanation: The infection reaches all healthy plants in 4 minutes.
Input: ([[2, 1, 1], [1, 3, 1], [0, 1, 1]],)
Expected Output: 5
Explanation: The obstacle forces the infection to take a longer route, but every healthy plant is still reachable.
Input: ([[2, 1, 1], [0, 3, 0], [1, 1, 1]],)
Expected Output: -1
Explanation: The bottom row is disconnected by empty cells and an obstacle, so those healthy plants can never be infected.
Input: ([[1, 1], [1, 1]],)
Expected Output: -1
Explanation: There is no initially infected plant to start the spread.
Input: ([],)
Expected Output: 0
Explanation: An empty grid requires no time.
Hints
- Treat all initially infected plants as starting points in the same BFS queue.
- Keep a count of healthy plants remaining; when one gets infected, decrement it.
Part 2: Infection Time for Every Plant
Constraints
- 0 <= m, n <= 200
- Each cell is one of: 0, 1, 2, 3
- Infection spreads in 4 directions only
Examples
Input: ([[2, 1, 1], [1, 1, 0], [0, 1, 1]],)
Expected Output: [[0, 1, 2], [1, 2, None], [None, 3, 4]]
Explanation: Each healthy plant is labeled with the minute it first becomes infected.
Input: ([[2, 1, 1], [1, 3, 1], [0, 1, 1]],)
Expected Output: [[0, 1, 2], [1, None, 3], [None, 5, 4]]
Explanation: The obstacle changes the infection path and therefore the infection times.
Input: ([[2, 1, 1], [0, 3, 0], [1, 1, 1]],)
Expected Output: [[0, 1, 2], [None, None, None], [-1, -1, -1]]
Explanation: The bottom row plants are unreachable, so they stay `-1`.
Input: ([[2, 1, 2], [1, 3, 1]],)
Expected Output: [[0, 1, 0], [1, None, 1]]
Explanation: With multiple initial infected plants, BFS starts from all of them at minute 0.
Input: ([],)
Expected Output: []
Explanation: An empty grid returns an empty result.
Hints
- Run multi-source BFS starting from all initially infected cells.
- Initialize healthy plants with `-1` and overwrite that value when they are first reached.
Part 3: Measure BFS Work and Peak Frontier
Constraints
- 0 <= m, n <= 200
- Each cell is one of: 0, 1, 2, 3
- Use 4-directional spread
Examples
Input: ([[2, 1], [1, 1]],)
Expected Output: (4, 8, 2)
Explanation: All 4 plant cells are processed, each processed cell contributes its in-bounds neighbor count, and the largest minute frontier has size 2.
Input: ([[2, 1, 0], [3, 1, 1]],)
Expected Output: (4, 10, 1)
Explanation: The infection moves along a single path, so the peak frontier size is 1.
Input: ([[2, 1, 2], [1, 3, 1]],)
Expected Output: (5, 11, 3)
Explanation: Two starting sources create a larger frontier on the next minute.
Input: ([[1]],)
Expected Output: (0, 0, 0)
Explanation: No BFS starts because there is no initially infected plant.
Input: ([],)
Expected Output: (0, 0, 0)
Explanation: An empty grid has no work and no frontier.
Hints
- A level-order BFS naturally gives you the frontier size for each minute.
- When processing a cell, inspect each of the 4 directions and count only those that stay inside the grid.
Part 4: Minimum Minutes with Diagonal Infection
Constraints
- 0 <= m, n <= 200
- Each cell is one of: 0, 1, 2, 3
- Infection spreads to all 8 adjacent cells
Examples
Input: ([[2, 0, 1], [0, 1, 0], [1, 0, 1]],)
Expected Output: 2
Explanation: The center plant is infected diagonally first, then it infects the remaining corners.
Input: ([[2, 3, 1], [3, 1, 3], [1, 3, 1]],)
Expected Output: 2
Explanation: Diagonal spread allows the infection to move even though orthogonal neighbors are blocked.
Input: ([[2, 1, 1], [1, 1, 0], [0, 1, 1]],)
Expected Output: 2
Explanation: Allowing diagonals reduces the total infection time compared with the 4-directional version.
Input: ([[1, 1], [1, 1]],)
Expected Output: -1
Explanation: No infection can start because there are no initially infected plants.
Input: ([],)
Expected Output: 0
Explanation: An empty grid needs no work.
Hints
- This is still a multi-source BFS problem; only the direction list changes.
- Use 8 directions instead of 4, and keep the same unreachable check as before.
Part 5: Sparse Infection on a Massive Grid
Constraints
- 1 <= rows, cols <= 10^9
- 0 <= len(healthy) + len(infected) + len(obstacles) <= 2 * 10^5
- All listed coordinates are unique, valid, and pairwise non-overlapping
Examples
Input: (1000000000, 1000000000, [(0, 1), (0, 2), (1, 2)], [(0, 0)], [(5, 5)])
Expected Output: 3
Explanation: The infection travels along the sparse chain of listed plants.
Input: (1000000, 1000000, [(1, 0), (1, 2), (2, 2)], [(0, 0), (0, 2)], [])
Expected Output: 2
Explanation: Two starting sources infect the nearby healthy plants first, then the final plant.
Input: (100, 100, [(0, 2)], [(0, 0)], [])
Expected Output: -1
Explanation: The empty cell at `(0, 1)` breaks the chain, so the healthy plant is unreachable.
Input: (50, 50, [], [(10, 10)], [(10, 11)])
Expected Output: 0
Explanation: There are no healthy plants to infect.
Input: (1, 1, [], [], [])
Expected Output: 0
Explanation: An empty sparse description requires no time.
Hints
- Do not allocate a `rows x cols` array; use sets or dictionaries keyed by coordinates.
- A healthy plant can only be infected if one of its 4 neighboring coordinates is already infected.