I spent the first few minutes just staring at the recovery rule because D=0 is a weird edge case where cells infect and recover in the same step.
Model the grid as a state machine and simulate day-by-day, tracking each cell's infection start day to know when it recovers. Use BFS or multi-source propagation to efficiently update newly infected cells, and stop when the grid state (infected set + recovery timers) is identical to the previous day.
Pro tip: Clarify edge cases upfront—like whether recovery happens before or after new infections on the same day—and mention that you'd use a queue to process only active infected cells, avoiding full-grid scans each day.
Ask about recovery timing (e.g., does a cell infected on day 0 recover on day D or D+1?), reinfection conditions, and whether immune cells block adjacency. Confirm that 'nothing changes' means both infection status and recovery timers are unchanged.
Use a 2D array for cell states (0=healthy, 1=infected, 2=immune) and a parallel array for infection start day (or remaining recovery days). Maintain a queue of currently infected cells to process only active infections.
At each day, process the queue: for each infected cell, if its recovery day is reached, mark it healthy; otherwise, attempt to infect adjacent healthy cells, adding them to the queue with their start day. Track changes to detect stability.
After each day, compare the current state (infected set and recovery timers) with the previous day. If no changes occurred, return the current day as the first stable day.
Discuss time complexity O(days * active infected cells) and space O(rows*cols). Mention potential optimizations like early termination if no infected cells remain or using a set for infected cells to speed up comparisons.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.