Multi-source BFS, which I figured out pretty quickly, but the threshold T tripped me up for a bit.
Model the grid as a 2D array and simulate the infection day by day using a multi-source BFS-like approach, but since infection depends on a threshold of infected neighbors, we must recompute the state each day until no changes occur. Use a copy of the grid to avoid overwriting cells within the same day, and count the days until a full pass yields no new infections.
Pro tip: Clarify edge cases upfront: what if T=0 (all healthy cells become infected immediately) or if the grid is already fully infected? Also, discuss potential optimizations like using a queue of cells whose neighbor counts changed, but for an interview, a clear O(m*n*days) simulation is often sufficient.
Restate the problem: infection spreads to all 8 neighbors each day, but a healthy cell only becomes infected if it has at least T infected neighbors. Ask clarifying questions about T, grid size, and initial state.
Decide to simulate day by day. Use a copy of the grid to compute the next state, ensuring that infections within the same day do not affect each other. Initialize a day counter.
For each healthy cell, count infected neighbors (8 directions). If count >= T, mark it infected in the next grid. Track whether any cell changed. If no changes, stop and return the day count.
Consider T=0 (all healthy become infected in one day), T>8 (no spread), and already fully infected grid (0 days). Discuss potential optimizations like maintaining a queue of cells with changing neighbor counts.
Time complexity is O(m*n*days) in the worst case. Space O(m*n). Mention that for large grids, a more efficient approach could use a queue to only process cells whose neighbor counts change, but it adds complexity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.