PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates competency in modeling and analyzing discrete-time grid-based state propagation with threshold-based infection rules and boundary conditions.

  • hard
  • OpenAI
  • Coding & Algorithms
  • Machine Learning Engineer

Compute time to infect all cells

Company: OpenAI

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are given an `n × m` grid representing people in a city. - Each cell is either **infected** (`1`) or **healthy** (`0`). - Two cells are **neighbors** if they share an edge (4-directional: up/down/left/right). - Infection spreads in **discrete time steps** (t = 0, 1, 2, ...). - At each time step, **all updates happen simultaneously**: - Any healthy cell becomes infected at the next step if it currently has **at least `K` infected neighbors**. - Infected cells stay infected. ### Task Return the **minimum number of time steps** until **all** cells are infected. - If the grid is already fully infected, return `0`. - If it is **impossible** for all cells to become infected, return `-1`. ### Input - `grid`: an `n × m` matrix of `0/1` - `K`: an integer threshold (`0 ≤ K ≤ 4`) ### Output - An integer: minimum time steps to infect all cells, or `-1` if impossible. ### Notes / Edge cases - If `K = 0`, then all healthy cells become infected after `1` step (unless already all infected). - A cell on the border has fewer than 4 neighbors. (Assume `1 ≤ n, m ≤ 200` and aim for an efficient solution.)

Quick Answer: This question evaluates competency in modeling and analyzing discrete-time grid-based state propagation with threshold-based infection rules and boundary conditions.

Given an `n x m` grid representing people in a city, simulate how an infection spreads and return the **minimum number of time steps until every cell is infected**. ## Input - `grid`: an `n x m` 2D array where each cell is either: - `1` — **infected**, or - `0` — **healthy**. - `K`: an integer threshold (the number of infected neighbors required to infect a healthy cell). Two cells are **neighbors** if they share an edge (up, down, left, or right). Diagonal cells are *not* neighbors. ## How the infection spreads Infection spreads in discrete time steps `t = 0, 1, 2, ...`. At each step, **all updates happen simultaneously**: - A **healthy** cell becomes infected at the next step if it currently has **at least `K` infected neighbors**. - Once **infected**, a cell stays infected forever. > **Simultaneity rule:** because updates apply at the same instant, a cell that becomes infected at time `t` cannot help infect its own neighbors until time `t + 1`. ## What to return Implement: ```python def solution(grid, K): ``` Return the **minimum number of time steps** needed for every cell in the grid to be infected, subject to these rules: - If the grid is **already fully infected**, return `0`. - If it is **impossible** for all cells to eventually become infected, return `-1`. - **Special case `K = 0`:** every healthy cell becomes infected after exactly `1` step. So return `1` whenever the grid contains at least one healthy cell, and `0` if the grid is already fully infected. (This holds even if there are no initially infected cells.) ## Constraints - `1 <= n, m <= 200` - `grid[i][j]` is either `0` or `1` - `0 <= K <= 4` ## Examples - `grid = [[1,0,0],[0,0,0]]`, `K = 1` → `3` (infection radiates outward one ring per step). - `grid = [[1,1],[1,1]]`, `K = 3` → `0` (already fully infected). - `grid = [[0,0],[0,0]]`, `K = 1` → `-1` (no infected cells and `K >= 1`, so nothing can ever spread). - `grid = [[0,0,0],[0,0,0]]`, `K = 0` → `1` (with `K = 0`, every healthy cell flips after one step).

Constraints

  • 1 <= n, m <= 200
  • grid[i][j] is either 0 or 1
  • 0 <= K <= 4

Examples

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

Expected Output: 3

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

Expected Output: 0

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

Expected Output: 1

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

Expected Output: -1

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

Expected Output: 3

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

Expected Output: -1

Input: ([[1]], 4)

Expected Output: 0

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

Expected Output: 2

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

Expected Output: 1

Hints

  1. For each healthy cell, keep track of how many infected neighbors it has seen so far. The moment this count reaches K, that cell is scheduled to become infected one step later.
  2. Instead of rescanning the whole grid after every time step, process newly infected cells with a queue starting from all initially infected cells.
Last updated: Apr 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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
  • AI Coding 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

  • Consistent Hashing Ring with Virtual Nodes for Shard Rebalancing - OpenAI (hard)
  • Infection Spread Simulation with Death Threshold - OpenAI (medium)
  • Spreading Contagion on a Grid - OpenAI (medium)
  • Implement A Contiguous Memory Allocator With Free-Block Coalescing - OpenAI (medium)
  • Aggregate Recent Message Events And Active Chats - OpenAI (hard)