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.