Compute Infection Time in a Grid
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates competence in grid-based graph traversal and simulation of propagation dynamics, focusing on state management, time-step modeling, and handling unreachable cells.
Constraints
- 0 <= m, n <= 200
- grid[i][j] is one of 0, 1, or 2
- Infection spreads only in the four cardinal directions
Examples
Input: ([[2,1,1],[1,1,0],[0,1,1]],)
Expected Output: 4
Explanation: The infection spreads outward each minute and reaches the last healthy cell after 4 minutes.
Input: ([[2,1,1],[0,1,1],[1,0,1]],)
Expected Output: -1
Explanation: At least one healthy cell is isolated by empty cells and can never be infected.
Input: ([[0,2,0]],)
Expected Output: 0
Explanation: There are no healthy cells at the start, so no time is needed.
Input: ([],)
Expected Output: 0
Explanation: An empty grid has no healthy cells to infect.
Input: ([[1]],)
Expected Output: -1
Explanation: There is one healthy cell and no infected cell to start the spread.
Input: ([[2,1,1],[1,1,1],[1,1,2]],)
Expected Output: 2
Explanation: Infection starts from two cells and spreads from both sides, infecting the whole grid in 2 minutes.