Problem · Matrix

Plant Infection Simulation, Part 4: Death Countdown

Learn this problem
HardOpenAIFULLTIMEPHONE SCREEN

Problem statement

This part adds a death countdown to the infection-and-recovery simulation.

Each input cell is X (infected), . (healthy), or I (immune). Immune cells never become infected and never transmit infection. Dead cells are terminal and never transmit infection.

Daily Update Rules

At the start of each day, count infected cells among each cell's eight horizontal, vertical, and diagonal neighbors. Using that start-of-day state:

  • A healthy cell with at least infectionThreshold infected neighbors becomes infected.
  • Any non-immune, non-dead cell with at least deathThreshold infected neighbors starts a death countdown if it does not already have one.

Infection and countdown starts are simultaneous. A cell can be infected while a death countdown is active. If it is infected, it remains contagious until it dies.

Recovery, Death, and Termination

A cell infected on day d normally recovers into I at the end of day d + duration. A cell whose death countdown starts on day d dies at the end of day d + duration. Once a countdown starts, that cell cannot recover. Infection spread is evaluated before recovery or death. Initially infected cells have infection day 0.

Continue until no infected cells and no active death countdowns remain. Return [daysUntilStable, totalDeaths].

Interview Sequence

What the interview report shared

The final reported part applied a death threshold to every non-immune cell, including healthy cells. The author clarified that the countdown begins the first time the threshold is met, uses the same D as recovery, prevents later recovery, and leaves an infected countdown cell contagious until death. The requested result was the elapsed day count together with the total number of deaths.

Function

infectionDaysAndDeaths(grid: String[], infectionThreshold: int, duration: int, deathThreshold: int) → int[]

Examples

Example 1

grid = ["X."]infectionThreshold = 2duration = 1deathThreshold = 1return = [2, 1]

On day 1, the healthy cell has one infected neighbor, so it starts a death countdown but does not meet the infection threshold. The initial infection recovers. The countdown finishes at the end of day 2, producing one death.

More OpenAI problems

drafts saved locally
public int[] infectionDaysAndDeaths(String[] grid, int infectionThreshold, int duration, int deathThreshold) {
  // write your code here
}
grid["X."]
infectionThreshold2
duration1
deathThreshold1
expected[2, 1]
checking account