Implement a grid infection simulator.
Base problem:
-
You are given a 2D grid.
-
X
means infected.
-
.
means healthy.
-
Each day, every currently infected cell simultaneously infects all 8 neighbors that are still healthy.
-
A cell infected on the current day cannot spread until the next day.
-
Return the number of days until the system becomes stable, meaning no new cells are infected.
Required edge cases:
-
Empty grid
-
No initial infected cells
-
Already fully infected grid
-
Single row or single column
-
Multiple infection sources
-
1 x 1
grid
Common follow-ups:
-
Add immune cells
I
that can never be infected and block spread.
-
Add recovery: an infected cell becomes immune after
D
days, and you must return the day when no active infected cells remain.
-
Add threshold infection: a healthy cell becomes infected only if at least
T
of its 8 neighbors were infected at the start of the day.
-
Add a countdown or death state, which requires careful synchronous state transitions.
The main skills tested are multi-source BFS, simulation with synchronous updates, boundary handling, and off-by-one correctness.