Google·Software Engineer·Onsite - Coding / Algorithms
- Given a 2D grid where cells can be healthy, infected, recovered, or walls, compute the state of the grid after exactly one step of infection spread. Healthy cells become infected if any orthogonal neighbor is infected; all other cells stay the same. Updates are simultaneous.
- Extend the single-step simulation to run for k steps, where walls permanently block infection spread and recovered cells are immune. Return the grid after k steps.
- Add a recovery mechanic: each infected cell tracks how many steps it has been infected. After exactly d steps of infection, the cell recovers permanently. Simulate k steps and return the final grid.
- Combine all previous rules and add early stopping: halt the simulation as soon as the grid reaches a fixed point where no further changes are possible. A fixed point requires both that no healthy cell is adjacent to an infected cell AND that no infected cells remain. Return the final grid and the actual number of steps taken.
“Pretty clean setup, just scan every cell and check neighbors.”