Simulate Infection Spread on a Grid
OpenAIMachine Learning Engineer · Technical ScreenLast updated
Implement a discrete-time cellular automaton simulating epidemic spread on a 2D grid with multi-day infection states, immunity waning, and mortality rules.
Problem Statement
You are tasked with building a discrete-time simulation of an epidemic spreading through an R x C population grid over a given number of days.
Each cell on the grid is in one of five base states:
.: Empty space (cannot contract or spread the infection)S: Susceptible individualI: Infected individualR: Recovered individual (temporarily immune)D: Deceased individual (permanent terminal state)
Simulation Rules
All state transitions occur synchronously at discrete daily steps based on the state of the grid at the beginning of the day:
-
Infection Dynamics:
- An infected individual remains infected for exactly
t_infdays. - Any
Scell with at leastk_transadjacent infected neighbors (considering all 8 directions: horizontal, vertical, diagonal) contracts the virus and transitions toIat the end of the day, starting witht_infdays remaining.
- An infected individual remains infected for exactly
-
Recovery and Mortality:
- When an infected cell reaches the end of its
t_inf-th day of infection:- If it has at least
k_deathcurrently infected neighbors (8-directional) at that time step, the individual dies and transitions toD. - Otherwise, the individual recovers and transitions to
R, starting witht_recdays of immunity remaining.
- If it has at least
- When an infected cell reaches the end of its
-
Immunity Waning:
- A recovered individual remains immune for
t_recdays. - At the end of its
t_rec-th day, ift_rec > 0, the individual loses immunity and transitions back toS. - If
t_rec == 0, a recovering individual transitions directly toSinstead ofR.
- A recovered individual remains immune for
-
Terminal / Inactive States:
- Empty cells (
.) and deceased individuals (D) never change state.
- Empty cells (
Initially, all 'I' cells in the starting grid have t_inf days remaining, and any 'R' cells have t_rec days remaining.
Write a function simulate_infection(grid, days, t_inf, t_rec, k_trans, k_death) that returns the grid state after days full days of simulation, where each cell is represented by its base character ('.' | 'S' | 'I' | 'R' | 'D').
Function Signature
def simulate_infection(
grid: list[list[str]],
days: int,
t_inf: int,
t_rec: int,
k_trans: int,
k_death: int
) -> list[list[str]]:
pass
Constraints
1 <= R, C <= 1000 <= days <= 10001 <= t_inf <= 500 <= t_rec <= 501 <= k_trans <= 81 <= k_death <= 8grid[r][c]is one of['.', 'S', 'I', 'R', 'D']
Examples
Example 1
grid = [
["S", "S", "."],
["S", "I", "."],
[".", ".", "."]
]
days = 2
t_inf = 2
t_rec = 1
k_trans = 1
k_death = 2
# Day 1:
# (1, 1) was I with 2 days left -> now 1 day left.
# (0, 0), (0, 1), (1, 0) are S and have 1 infected neighbor (>= k_trans=1) -> become I with 2 days left.
# Day 2:
# (1, 1) finishes infection (reached 0 days left). It has 3 currently infected neighbors: (0,0), (0,1), (1,0). Since 3 >= k_death (2), it dies -> 'D'.
# (0, 0), (0, 1), (1, 0) were I with 2 days left -> now 1 day left.
output = [
["I", "I", "."],
["I", "D", "."],
[".", ".", "."]
]
Example 2
grid = [["I"]]
days = 3
t_inf = 1
t_rec = 1
k_trans = 1
k_death = 1
# Day 1: (0, 0) has 0 infected neighbors ( R with 1 day.
# Day 2: (0, 0) finishes immunity -> transitions to S.
# Day 3: (0, 0) remains S (no infected neighbors).
output = [["S"]]
Hints
Hint 1
Separate the grid representation into state and timer so you can accurately track cell aging alongside cell status.
Hint 2
Ensure all neighbor counts and transition conditions are evaluated simultaneously using the state at step t before writing changes to step t+1.
Hint 3
Pay special attention to order of operations on the last day of infection: count infected neighbors in the current time step before resolving the transition to 'D' or 'R'.
Follow-up questions
- How would you optimize the simulation if R and C were 10,000 but the number of non-susceptible cells was sparse?
- If infection transmission were probabilistic (e.g., each infected neighbor independently transmits with probability p), how would you structure test assertions to validate correctness?
- How would you detect periodic cycles or early stabilization to short-circuit the simulation before reaching `days`?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.