Compute Plant Infection Time
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of graph traversal and grid-based state propagation, including concepts like breadth-first search, reachability, obstacle handling, and edge-case reasoning.
Constraints
- 0 <= m, n <= 200
- Each grid cell is one of -1, 0, 1, or 2
- The input is a rectangular grid
- An O(m * n) breadth-first search solution is expected for large grids
Examples
Input: ([[2, 1, 1], [1, 1, 0], [0, 1, 1]],)
Expected Output: 4
Explanation: The infection spreads layer by layer and reaches the last healthy plant after 4 minutes.
Input: ([[2, 1, -1], [-1, 1, 1], [1, -1, 1]],)
Expected Output: -1
Explanation: The healthy plant at the bottom-left is isolated by walls, so not every healthy plant can be infected.
Input: ([[0, 2, -1], [0, 0, 2]],)
Expected Output: 0
Explanation: There are no healthy plants initially, so no time is needed.
Input: ([[1, 1], [1, -1]],)
Expected Output: -1
Explanation: There are healthy plants but no infected plants to start the spread.
Input: ([[2, 1, 1], [1, -1, 1], [1, 1, 2]],)
Expected Output: 2
Explanation: Two infection sources spread simultaneously, so all reachable healthy plants are infected in 2 minutes.
Input: ([],)
Expected Output: 0
Explanation: An empty grid contains no healthy plants, so the answer is 0.
Hints
- Treat all initially infected plants as starting points in the same BFS queue.
- Count how many healthy plants exist, and decrease that count as they become infected.