21Infection spread on a grid

hardpython
60:00

21 Infection spread on a grid

OpenAI's most reported coding question is a grid simulation of an infection (11 sources), in phone screens and onsites for software, ML and research engineers. Candidates describe five parts in 60 minutes, each one adding a rule (2 reports): how long until everyone is infected, then immune cells, then a countdown after which the infected recover and become immune, then a death state (1 report). Most get through three or four parts (5 reports). Two traps from the reports: the cells infected at the start recover on the same countdown (1 report), and the death count the fourth part returns is easy to get wrong (1 report). A community guide to Anthropic's online assessment lists a version where a cell catches it once K of its neighbors have it (1 source), and an Airbnb report lists infection spread time on a grid (1 source). This is practice in that shape, not a leaked question.

Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/21_infection_grid -k level1. Earlier levels must keep passing.

The grid. A list of equal-length strings: . is a healthy cell, I an infected one, # an immune one (from level 2). Neighbors share a side: up, down, left and right, never diagonals. Day 0 is the grid you're given. Each day every cell changes at the same moment, based only on the grid of the day before, so a cell infected today starts infecting tomorrow.

Level 1. Write days_to_infect_all(grid, k=1) -> int. A healthy cell gets infected on a day when at least k of its neighbors were infected the day before. Return the first day on which every cell is infected: 0 if they all are on day 0, -1 if that day never comes.

Level 2. Immune cells. The grid can now hold #. An immune cell never gets infected and never infects anyone. Return the first day on which every cell that isn't immune is infected, or -1. A grid with no healthy cells returns 0.

Level 3. Recovery. Write simulate(grid, recover_after, k=1) -> tuple[int, list[str]]. The infection spreads by the level 1 and 2 rules. A cell infected on day s recovers on day s + recover_after: it becomes R, can't be infected again and stops infecting. The cells infected on day 0 recover on day recover_after. Recovery happens at the same moment as spreading, so a cell that recovers on day d can still infect its neighbors on day d: they read day d - 1. Return the first day on which no cell is infected, and the grid on that day as a list of strings. If nobody is infected at the start, return (0, grid).

Level 4. Deaths. simulate gets lethal=None and die_after=None. A cell that catches it from lethal or more infected neighbors (counted on the same day-before grid that infected it) is a severe case: it dies on day s + die_after and becomes X instead of recovering. Dead cells never get infected and never infect. The cells infected on day 0 are never severe. lethal=None means nobody dies, and die_after=None means a severe case dies after recover_after days. The X cells are the death count.

Performance counts. Rescanning the whole grid every day costs rows times columns per day, even when one cell changes. Only the neighbors of yesterday's new infections can catch it today, and every recovery and death is known the day the cell is infected, so keep a frontier and a schedule instead (q-ant-130 asks for better than a full scan per step).

One candidate reached a fifth part: you may burn one whole row or column to stop the spread, and you have to find the burn that leaves the fewest dead (1 report). It isn't tested here. If you finish early, talk through how you'd search for it.

Graded the way OpenAI says it grades: design, code quality, performance, tests (1 source). Each level should be a small change to the last one, not a rewrite.

solution.py
Run the tests to see which pass.