PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement grid-based simulation with multi-state cellular automaton logic, testing practical application of BFS/iterative traversal and careful simultaneous state transitions. It falls within the Coding & Algorithms domain and assesses how well engineers handle complex rule sets, neighbor counting, and convergence detection on 2D grids.

  • medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Infection Spread Simulation with Death Threshold

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid that simulates the spread of an infection over discrete days. Each cell holds one of four states: - `0` — uninfected (healthy and susceptible) - `1` — infected - `2` — immune (recovered; permanently safe and can never be infected again) - `3` — dead (permanent; never changes) Two integer parameters govern the dynamics: - `infectThreshold` (`K_inf`): an uninfected cell becomes infected on the next day if **at least `infectThreshold`** of its neighbors are infected on the current day. - `deathThreshold` (`K_death`): if a cell is infected and, at the moment it becomes infected, **at least `deathThreshold`** of its neighbors are also infected, the cell is marked for death; when its recovery time arrives it dies instead of recovering. Every infected cell stays infected for exactly **one full day** and then resolves on the following day: - If the cell was **not** marked for death, it becomes immune (`2`) on the next day. - If the cell **was** marked for death, it becomes dead (`3`) on the next day instead of recovering. Neighbors are the up to 8 cells horizontally, vertically, and diagonally adjacent (Moore neighborhood). Cells outside the grid are not neighbors. ### Day-transition rules (applied simultaneously to every cell) Compute the next day's grid from the current grid as follows. All cells transition at the same instant — base every decision on the **current** day's values, not on partially-updated values. For each cell: 1. **`0` (uninfected):** Let `c` be the number of its neighbors that are `1` (infected) on the current day. If `c >= infectThreshold`, the cell becomes `1` (infected) next day. Otherwise it stays `0`. At the instant a cell becomes infected, if `c >= deathThreshold` as well, the cell is permanently marked for death (this mark is remembered until the cell resolves). 2. **`1` (infected):** The cell has now completed its one infected day and resolves next day. If it was marked for death, it becomes `3` (dead); otherwise it becomes `2` (immune). 3. **`2` (immune):** Stays `2` forever. 4. **`3` (dead):** Stays `3` forever. A cell that is uninfected and infected on the same transition uses the same neighbor count `c` for both the infection check (`>= infectThreshold`) and the death-mark check (`>= deathThreshold`). The simulation runs until the grid reaches a steady state — a day on which applying the transition rules produces a grid identical to the current one (no cell changes). Return the grid in that final, stable state. ### Worked example `infectThreshold = 1`, `deathThreshold = 1`. Day 0: ``` 1 0 0 0 0 0 0 0 0 ``` Day 1 (cell `(0,0)` was infected for its one day, has no infected neighbors so it is not marked for death, and recovers to `2`; cells `(0,1)`, `(1,0)`, `(1,1)` each had `1` infected neighbor, met `infectThreshold = 1`, so they become infected — and since their infected-neighbor count `1` also meets `deathThreshold = 1`, they are all marked for death): ``` 2 1 0 1 1 0 0 0 0 ``` Day 2 (the three cells infected on Day 1 were all marked for death, so they become `3`; their uninfected neighbors with `>= 1` infected neighbor on Day 1 become infected and are likewise marked for death): ``` 2 3 1 3 3 1 1 1 1 ``` Day 3 — final stable state (the newly infected cells were marked for death and become `3`; no uninfected cells remain reachable, so the grid no longer changes): ``` 2 3 3 3 3 3 3 3 3 ``` Return the Day 3 grid. ### Input / Output - **Input:** a 2D integer array `grid` (`m x n`, each entry in `{0, 1, 2, 3}`), an integer `infectThreshold`, and an integer `deathThreshold`. - **Output:** the 2D integer array representing the grid once it has reached a stable state (a day whose transition leaves the grid unchanged). ### Constraints - `1 <= m, n <= 200` - `1 <= infectThreshold <= 8` - `1 <= deathThreshold <= 8` - Every cell of the input grid is one of `0`, `1`, `2`, `3`. - The simulation is guaranteed to converge to a stable state.

Quick Answer: This question evaluates a candidate's ability to implement grid-based simulation with multi-state cellular automaton logic, testing practical application of BFS/iterative traversal and careful simultaneous state transitions. It falls within the Coding & Algorithms domain and assesses how well engineers handle complex rule sets, neighbor counting, and convergence detection on 2D grids.

You are given an `m x n` grid that simulates the spread of an infection over discrete days. Each cell holds one of four states: - `0` — uninfected (healthy and susceptible) - `1` — infected - `2` — immune (recovered; permanently safe and can never be infected again) - `3` — dead (permanent; never changes) Two integer parameters govern the dynamics: - `infectThreshold` (`K_inf`): an uninfected cell becomes infected on the next day if **at least `infectThreshold`** of its neighbors are infected on the current day. - `deathThreshold` (`K_death`): if a cell is infected and, at the moment it becomes infected, **at least `deathThreshold`** of its neighbors are also infected, the cell is marked for death; when its recovery time arrives it dies instead of recovering. Every infected cell stays infected for exactly **one full day** and then resolves on the following day: - If the cell was **not** marked for death, it becomes immune (`2`) on the next day. - If the cell **was** marked for death, it becomes dead (`3`) on the next day instead of recovering. Neighbors are the up to 8 cells horizontally, vertically, and diagonally adjacent (Moore neighborhood). Cells outside the grid are not neighbors. ### Day-transition rules (applied simultaneously to every cell) Compute the next day's grid from the current grid. All cells transition at the same instant — base every decision on the **current** day's values, not on partially-updated values. For each cell: 1. **`0` (uninfected):** Let `c` be the number of its neighbors that are `1` (infected) on the current day. If `c >= infectThreshold`, the cell becomes `1` (infected) next day. Otherwise it stays `0`. At the instant it becomes infected, if `c >= deathThreshold` as well, the cell is permanently marked for death (remembered until the cell resolves). 2. **`1` (infected):** The cell has completed its one infected day and resolves next day — `3` (dead) if it was marked for death, otherwise `2` (immune). 3. **`2` (immune):** Stays `2` forever. 4. **`3` (dead):** Stays `3` forever. A cell that becomes infected on a transition uses the same neighbor count `c` for both the infection check (`>= infectThreshold`) and the death-mark check (`>= deathThreshold`). Cells infected in the initial grid are treated as not marked for death. The simulation runs until the grid reaches a steady state — a day on which applying the transition rules produces a grid identical to the current one. Return the grid in that final, stable state. ### Input / Output - **Input:** a 2D integer array `grid` (`m x n`, each entry in `{0, 1, 2, 3}`), an integer `infectThreshold`, and an integer `deathThreshold`. - **Output:** the 2D integer array once the grid has reached a stable state. ### Constraints - `1 <= m, n <= 200` - `1 <= infectThreshold <= 8` - `1 <= deathThreshold <= 8` - Every cell of the input grid is one of `0`, `1`, `2`, `3`. - The simulation is guaranteed to converge to a stable state.

Constraints

  • 1 <= m, n <= 200
  • 1 <= infectThreshold <= 8
  • 1 <= deathThreshold <= 8
  • Every cell of the input grid is one of 0, 1, 2, 3.
  • The simulation is guaranteed to converge to a stable state.

Examples

Input: ([[1,0,0],[0,0,0],[0,0,0]], 1, 1)

Expected Output: [[2,3,3],[3,3,3],[3,3,3]]

Explanation: The worked example. The seed at (0,0) recovers to 2 (no infected neighbors, not marked). Its three neighbors get infected with c=1 >= deathThreshold=1, so they are marked for death and become 3; the infection then spreads to the rest, all marked for death.

Input: ([[0,0,0],[0,1,0],[0,0,0]], 1, 2)

Expected Output: [[2,2,2],[2,2,2],[2,2,2]]

Explanation: infectThreshold=1 but deathThreshold=2. The center recovers to 2. Each of the 8 surrounding cells has exactly 1 infected neighbor: 1 >= infectThreshold so they get infected, but 1 < deathThreshold so they are NOT marked, and they recover to 2.

Input: ([[1,0,0],[0,0,0],[0,0,0]], 8, 8)

Expected Output: [[2,0,0],[0,0,0],[0,0,0]]

Explanation: The corner cell has only 3 neighbors, none of which can reach infectThreshold=8, so no neighbor is ever infected. The seed recovers to 2 and the rest stay 0.

Input: ([[2,3],[3,2]], 1, 1)

Expected Output: [[2,3],[3,2]]

Explanation: Immune (2) and dead (3) cells never change, so the grid is already stable.

Input: ([[0,0],[0,0]], 1, 1)

Expected Output: [[0,0],[0,0]]

Explanation: No infected cells, nothing spreads — already a stable state.

Input: ([[1]], 1, 1)

Expected Output: [[2]]

Explanation: A 1x1 grid: the lone infected cell has 0 infected neighbors, so c=0 < deathThreshold; it is not marked and recovers to 2.

Input: ([[1,0,1],[0,0,0],[1,0,1]], 2, 3)

Expected Output: [[2,2,2],[2,3,2],[2,2,2]]

Explanation: The four corners are infected. The center has 4 infected neighbors: 4 >= infectThreshold=2 (infected) and 4 >= deathThreshold=3 (marked for death), so it becomes 3. The corners have 0 infected neighbors and recover to 2; edge cells each see only 2 infected neighbors so they too get infected but with 2 < deathThreshold recover to 2.

Hints

  1. Simulate day by day. Each day, build a brand-new grid by reading only the current day's values, so simultaneous transitions don't interfere with each other.
  2. The grid alone can't tell you whether an infected (`1`) cell was marked for death. Carry a parallel boolean mark alongside the grid: when a cell becomes infected with `c >= deathThreshold`, set its mark; when it resolves, mark -> dead (`3`), no mark -> immune (`2`).
  3. Use the same neighbor count `c` for both the infection check (`>= infectThreshold`) and the death-mark check (`>= deathThreshold`) on the same transition.
  4. Stop when a transition produces a grid identical to the current one. Cells initially infected in the input are treated as not marked for death.
Last updated: Jun 24, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Spreading Contagion on a Grid - OpenAI (medium)
  • Streaming Entropy with Numerical Stability - OpenAI (hard)
  • Implement a Distributed Rate Limiter - OpenAI (medium)
  • Compute Plant Infection Time - OpenAI (medium)
  • Implement IP Address Arithmetic - OpenAI (hard)