PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's understanding of graph traversal and grid-based state propagation, including concepts like breadth-first search, reachability, obstacle handling, and edge-case reasoning.

  • medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Compute Plant Infection Time

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid representing a garden. Each cell is one of the following: - `0`: empty ground - `1`: a healthy plant - `2`: an infected plant - `-1`: a wall or blocked cell At the end of each minute, every infected plant spreads the infection to all orthogonally adjacent healthy plants: up, down, left, and right. Infection cannot pass through walls or empty cells. Implement a function that returns the minimum number of minutes required until no more plants can be infected. If every healthy plant can eventually be infected, return that number of minutes. If at least one healthy plant can never be reached by the infection, return `-1`. Clarify and handle edge cases such as: - No healthy plants initially - No infected plants initially - Multiple infected plants initially - Walls that isolate parts of the garden - Large grids where an efficient breadth-first search is expected

Quick Answer: This question evaluates a candidate's understanding of graph traversal and grid-based state propagation, including concepts like breadth-first search, reachability, obstacle handling, and edge-case reasoning.

You are given a garden as an m x n grid. Each cell contains one of four values: 0 for empty ground, 1 for a healthy plant, 2 for an infected plant, and -1 for a wall or blocked cell. At the end of each minute, every infected plant spreads the infection to all orthogonally adjacent healthy plants (up, down, left, right). Infection cannot move through walls or empty ground. Return the minimum number of minutes needed until no more plants can be infected if every healthy plant can eventually become infected. If at least one healthy plant can never be reached, return -1. If there are no healthy plants initially, return 0.

Constraints

  • 0 <= m, n <= 200
  • Each grid cell is one of -1, 0, 1, or 2
  • The input is a rectangular grid
  • An O(m * n) breadth-first search solution is expected for large grids

Examples

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

Expected Output: 4

Explanation: The infection spreads layer by layer and reaches the last healthy plant after 4 minutes.

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

Expected Output: -1

Explanation: The healthy plant at the bottom-left is isolated by walls, so not every healthy plant can be infected.

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

Expected Output: 0

Explanation: There are no healthy plants initially, so no time is needed.

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

Expected Output: -1

Explanation: There are healthy plants but no infected plants to start the spread.

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

Expected Output: 2

Explanation: Two infection sources spread simultaneously, so all reachable healthy plants are infected in 2 minutes.

Input: ([],)

Expected Output: 0

Explanation: An empty grid contains no healthy plants, so the answer is 0.

Hints

  1. Treat all initially infected plants as starting points in the same BFS queue.
  2. Count how many healthy plants exist, and decrease that count as they become infected.
Last updated: May 30, 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

  • Implement IP Address Arithmetic - OpenAI (hard)
  • 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)