Infection on a grid
OpenAI's most reported coding question is a grid simulation of an infection, from phone screens and onsites (3 reports). It starts like this:
The grid is a list of equal-length strings.
.is a healthy cell andIis an infected one. Cells touch up, down, left and right.Each day, a healthy cell becomes infected if at least
kof its neighbors were infected the day before. Writedays_to_infect_all(grid, k=1): the first day on which every cell is infected. Return0if every cell already is, and-1if that day never comes.
Grid Returns ["I.", ".."]2["I..", "...", "..."]4["II", "II"]0["..."]-1, because nothing is infected, so nothing spreads
Candidates describe five parts in 60 minutes, each adding a rule, and most get through three or four (2 reports). After this one come immune cells, then recovery after a countdown, then deaths, and last, burning one whole row or column to stop the spread.
Under the story it's breadth-first search: each day the infection spreads one ring outward, from every infected cell at once. You don't write it yet. It needs two ideas first: the grid as a graph, and the search itself. At the end you solve the whole problem, level by level, in an interview simulation with a 60-minute timer.
This lesson builds on hash maps and queues: the search keeps a set of the cells it has seen and a queue of the cells to visit next. The same breadth-first search runs the web crawler in the coding builds, where small systems grow in levels, over links instead of grid cells.