Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Simulate Infection Propagation

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Simulate Infection Propagation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

OpenAI Codex is evaluating a plant-health simulation represented as a rectangular grid. Implement a synchronous daily simulation that propagates infection while modeling recovery, temporary immunity, and mortality.

Each cell is one of four strings: "S" for susceptible, "I" for infected, "R" for temporarily immune after recovery, or "D" for dead. Only orthogonal neighbors count. Every cell is evaluated from the state at the start of the day, so updates must not affect other cells during that same day. All initially infected cells have infection age zero.

For each day:

  1. A susceptible cell becomes infected if it has at least infection_threshold infected neighbors.
  2. An infected cell dies if it has at least mortality_threshold infected neighbors. Otherwise, its infection age increases. It recovers when its new age reaches recovery_days.
  3. A recovered cell remains immune for immunity_days, then becomes susceptible.
  4. Dead cells remain dead.

Return the grid after exactly days days.

Formal Specification

Implement simulate_infection(grid, days, infection_threshold, recovery_days, immunity_days, mortality_threshold). grid is a non-empty M x N matrix of strings, and the result has the same dimensions. The input grid must not be modified.

Constraints

  • 1 <= M, N <= 500
  • 0 <= days <= 10^4
  • 1 <= infection_threshold <= 4
  • 1 <= mortality_threshold <= 4
  • 1 <= recovery_days <= 10^4
  • 1 <= immunity_days <= 10^4
  • Every cell is one of "S", "I", "R", or "D"

Function Signature

def simulate_infection(grid, days, infection_threshold, recovery_days, immunity_days, mortality_threshold):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output