Simulate Infection Spread on a Grid
Company: OpenAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to implement and reason about discrete-time, grid-based simulations with multi-state cells and neighbor-dependent transitions, including state aging, immunity, and death tracking.
Part 1: Basic Infection Spread
Constraints
- 0 <= len(grid) <= 30
- 0 <= len(grid[0]) <= 30 if grid is non-empty
- Each row has the same length
- Grid characters are only '.' or 'X'
- 1 <= T <= 8
Examples
Input: (["X..", "...", "..."], 1)
Expected Output: 2
Explanation: The infection spreads from the top-left corner outward. Three cells infect on day 1, and the remaining cells infect on day 2.
Input: (["...", "..."], 1)
Expected Output: 0
Explanation: There are no infected cells initially, so the grid is already stable.
Input: (["...", ".X.", "..."], 8)
Expected Output: 0
Explanation: Each healthy cell has at most 1 infected neighbor, so none meet the threshold of 8.
Input: (["X." , ".X"], 2)
Expected Output: 1
Explanation: Each healthy cell has exactly 2 infected neighbors, so both become infected after one day.
Input: (["X"], 1)
Expected Output: 0
Explanation: A single infected cell has no healthy neighbors to infect.
Hints
- Simulate the process day by day, but collect all cells that should change before modifying the grid.
- There are only 8 possible neighbor directions, so a direct neighbor count is enough.
Part 2: Infection Spread with Immune Cells
Constraints
- 0 <= len(grid) <= 30
- 0 <= len(grid[0]) <= 30 if grid is non-empty
- Each row has the same length
- Grid characters are only '.', 'X', or 'I'
- 1 <= T <= 8
Examples
Input: (["X.I", "...", "I.."], 1)
Expected Output: 2
Explanation: The immune cells remain unchanged, while infection still spreads through other reachable healthy cells over two days.
Input: (["I.I", "III"], 1)
Expected Output: 0
Explanation: There are no infected cells at all, so the grid is already stable.
Input: (["XI", ".I"], 2)
Expected Output: 0
Explanation: The only healthy cell has just 1 infected neighbor, which is below the threshold.
Input: (["X.I"], 1)
Expected Output: 1
Explanation: The middle healthy cell has one infected neighbor, so it becomes infected after one day.
Input: (["I"], 1)
Expected Output: 0
Explanation: A single immune cell never changes.
Hints
- Only '.' cells are candidates to change. 'I' cells should stay exactly as they are.
- When counting neighbors, only 'X' contributes to infection spread.
Part 3: Infection Spread with Recovery
Constraints
- 0 <= len(grid) <= 30
- 0 <= len(grid[0]) <= 30 if grid is non-empty
- Each row has the same length
- Grid characters are only '.', 'X', or 'I'
- 1 <= T <= 8
- 1 <= D <= 30
Examples
Input: (["X"], 1, 1)
Expected Output: 1
Explanation: The single infected cell recovers after one day, so there are no infected cells left on day 1.
Input: (["X..", "...", "..."], 1, 2)
Expected Output: 4
Explanation: The infection spreads outward for two days, then infected cells gradually recover until none remain after day 4.
Input: (["...", ".X.", "..."], 2, 2)
Expected Output: 2
Explanation: No healthy cell ever reaches the threshold of 2 infected neighbors, so the original infected cell simply recovers after two days.
Input: (["XI", ".I"], 1, 2)
Expected Output: 3
Explanation: The lower-left healthy cell is infected on day 1, then the original infected cell recovers on day 2, and the new infected cell recovers on day 3.
Input: (["..", "II"], 1, 3)
Expected Output: 0
Explanation: There are no infected cells initially.
Hints
- Store the infection age separately from the visible grid state.
- Be careful with simultaneity: newly infected cells should not age or recover on the same day they appear.
Part 4: Infection Spread with Recovery and Delayed Death
Constraints
- 0 <= len(grid) <= 30
- 0 <= len(grid[0]) <= 30 if grid is non-empty
- Each row has the same length
- Initial grid characters are only '.', 'X', or 'I'
- 1 <= T <= 8
- 1 <= K <= 8
- 1 <= D <= 30
Examples
Input: (["..", "II"], 1, 2, 2)
Expected Output: (0, 0)
Explanation: There are no infected cells initially, so the stable day is 0 and nobody dies.
Input: (["X"], 1, 1, 2)
Expected Output: (1, 0)
Explanation: The infected cell recovers after one day. There is no death because the exposure threshold is never met.
Input: (["XX", "X."], 1, 1, 3)
Expected Output: (2, 1)
Explanation: The healthy cell is infected and scheduled to die on day 1. On day 2, it would recover and die at the same time, and death wins.
Input: (["XX", "X."], 4, 2, 3)
Expected Output: (2, 1)
Explanation: The healthy cell is scheduled to die because it has 3 infected neighbors, but it never becomes infected because T is 4. The original infected cells recover on day 2, so days_until_stable is 2. The scheduled healthy cell dies later, making total_deaths 1.
Input: (["I"], 1, 3, 1)
Expected Output: (0, 0)
Explanation: A single immune cell never changes and can never be scheduled to die.
Hints
- Track infection age and death-schedule age independently; they are two different timers.
- Use the current day's infected cells to decide both new infections and new death schedules, then apply deaths last so death can override recovery or infection.