Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Infection Simulation Stop Time

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

Your question is Infection Simulation Stop Time. 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 simulation tooling can model how a condition spreads through a discrete region. Given an M x N grid, calculate how many days are required for infection to spread from all initially infected cells to every reachable healthy cell.

Each cell contains:

  • 0, an empty cell that cannot be infected
  • 1, a healthy cell
  • 2, an initially infected cell

At the end of each day, every infected cell infects its healthy neighbors in the four cardinal directions. Newly infected cells begin spreading on the following day. Return the number of days until no healthy cells remain reachable. Return -1 if at least one healthy cell can never be infected.

Formal Specification

Implement infect_days(grid), where grid is a non-empty list of non-empty lists containing only 0, 1, and 2. Return an integer. The function may modify grid in place.

Constraints

  • 1 <= M, N <= 500
  • grid contains only 0, 1, and 2
  • Infection spreads only up, down, left, and right
  • All initially infected cells spread simultaneously

Function Signature

def infect_days(grid):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output