Infection Spread Simulation with Death Threshold
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to implement grid-based simulation with multi-state cellular automaton logic, testing practical application of BFS/iterative traversal and careful simultaneous state transitions. It falls within the Coding & Algorithms domain and assesses how well engineers handle complex rule sets, neighbor counting, and convergence detection on 2D grids.
Constraints
- 1 <= m, n <= 200
- 1 <= infectThreshold <= 8
- 1 <= deathThreshold <= 8
- Every cell of the input grid is one of 0, 1, 2, 3.
- The simulation is guaranteed to converge to a stable state.
Examples
Input: ([[1,0,0],[0,0,0],[0,0,0]], 1, 1)
Expected Output: [[2,3,3],[3,3,3],[3,3,3]]
Explanation: The worked example. The seed at (0,0) recovers to 2 (no infected neighbors, not marked). Its three neighbors get infected with c=1 >= deathThreshold=1, so they are marked for death and become 3; the infection then spreads to the rest, all marked for death.
Input: ([[0,0,0],[0,1,0],[0,0,0]], 1, 2)
Expected Output: [[2,2,2],[2,2,2],[2,2,2]]
Explanation: infectThreshold=1 but deathThreshold=2. The center recovers to 2. Each of the 8 surrounding cells has exactly 1 infected neighbor: 1 >= infectThreshold so they get infected, but 1 < deathThreshold so they are NOT marked, and they recover to 2.
Input: ([[1,0,0],[0,0,0],[0,0,0]], 8, 8)
Expected Output: [[2,0,0],[0,0,0],[0,0,0]]
Explanation: The corner cell has only 3 neighbors, none of which can reach infectThreshold=8, so no neighbor is ever infected. The seed recovers to 2 and the rest stay 0.
Input: ([[2,3],[3,2]], 1, 1)
Expected Output: [[2,3],[3,2]]
Explanation: Immune (2) and dead (3) cells never change, so the grid is already stable.
Input: ([[0,0],[0,0]], 1, 1)
Expected Output: [[0,0],[0,0]]
Explanation: No infected cells, nothing spreads — already a stable state.
Input: ([[1]], 1, 1)
Expected Output: [[2]]
Explanation: A 1x1 grid: the lone infected cell has 0 infected neighbors, so c=0 < deathThreshold; it is not marked and recovers to 2.
Input: ([[1,0,1],[0,0,0],[1,0,1]], 2, 3)
Expected Output: [[2,2,2],[2,3,2],[2,2,2]]
Explanation: The four corners are infected. The center has 4 infected neighbors: 4 >= infectThreshold=2 (infected) and 4 >= deathThreshold=3 (marked for death), so it becomes 3. The corners have 0 infected neighbors and recover to 2; edge cells each see only 2 infected neighbors so they too get infected but with 2 < deathThreshold recover to 2.
Hints
- Simulate day by day. Each day, build a brand-new grid by reading only the current day's values, so simultaneous transitions don't interfere with each other.
- The grid alone can't tell you whether an infected (`1`) cell was marked for death. Carry a parallel boolean mark alongside the grid: when a cell becomes infected with `c >= deathThreshold`, set its mark; when it resolves, mark -> dead (`3`), no mark -> immune (`2`).
- Use the same neighbor count `c` for both the infection check (`>= infectThreshold`) and the death-mark check (`>= deathThreshold`) on the same transition.
- Stop when a transition produces a grid identical to the current one. Cells initially infected in the input are treated as not marked for death.