PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of grid-based graph traversal and propagation dynamics, testing competencies in breadth-first search concepts, simulation of state changes, and analysis of time and space complexity.

  • medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Compute infection spread time

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an `m x n` grid representing a population during an outbreak: - `0` = empty cell - `1` = healthy person - `2` = infected person Every minute, each infected person spreads the disease to any healthy person in the four adjacent cells: up, down, left, and right. Return the minimum number of minutes needed until no healthy person remains. If it is impossible to infect everyone, return `-1`. Example: - Input: `[[2,1,1],[1,1,0],[0,1,1]]` - Output: `4` Discuss the algorithm, its time complexity, and important edge cases.

Quick Answer: This question evaluates understanding of grid-based graph traversal and propagation dynamics, testing competencies in breadth-first search concepts, simulation of state changes, and analysis of time and space complexity.

You are given an m x n grid representing a population during an outbreak, where 0 means an empty cell, 1 means a healthy person, and 2 means an infected person. Every minute, each infected person spreads the disease to any healthy person in the four adjacent cells: up, down, left, and right. Return the minimum number of minutes needed until no healthy person remains. If it is impossible to infect every healthy person, return -1. If the grid is empty or there are no healthy people initially, return 0.

Constraints

  • 0 <= number of rows, number of columns <= 200
  • grid[i][j] is one of {0, 1, 2}
  • At most 40,000 cells in the grid

Examples

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

Expected Output: 4

Explanation: The infection spreads outward from the initial infected cell, and the last healthy person becomes infected after 4 minutes.

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

Expected Output: -1

Explanation: The healthy person in the bottom-left area is isolated by empty cells, so not everyone can be infected.

Input: [[0,2]]

Expected Output: 0

Explanation: There are no healthy people at the start, so the answer is 0.

Input: []

Expected Output: 0

Explanation: An empty grid contains no healthy people, so no time is needed.

Input: [[1]]

Expected Output: -1

Explanation: There is one healthy person and no infected person to start the spread, so infection is impossible.

Hints

  1. This is a shortest-spread problem on a grid, and all initially infected cells start spreading at the same time.
  2. Use a multi-source BFS: put every infected cell into the queue first, then expand level by level where each level represents one minute.
Last updated: Apr 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,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
  • 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

  • Simulate Infection Spread on a Grid - OpenAI (hard)
  • Implement Social Follow Recommendations - OpenAI (medium)
  • Build a Compose Rating Card - OpenAI (medium)
  • Generate Data Labeling Schedules - OpenAI (medium)
  • Convert IPv4 Ranges to CIDR Blocks - OpenAI (medium)