Compute time to infect all cells
Company: OpenAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: This question evaluates competency in modeling and analyzing discrete-time grid-based state propagation with threshold-based infection rules and boundary conditions.
Read the full OpenAI Machine Learning Engineer interview experience this question came from
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
Hints
- 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.
- Instead of rescanning the whole grid after every time step, process newly infected cells with a queue starting from all initially infected cells.
Community answers
Answer by EL