PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to reason about grid-based state propagation, algorithmic problem solving, and performance considerations when managing multi-step updates across a 2D matrix.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Compute time for virus to infect grid

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid representing the state of individuals in a lab. - `0` = empty cell (no one) - `1` = healthy individual - `2` = infected individual Every minute, any **healthy** individual (`1`) that is **4-directionally adjacent** (up/down/left/right) to an infected individual (`2`) becomes infected. Return the **minimum number of minutes** needed until **no healthy individuals remain**. If it is **impossible** to infect all healthy individuals, return `-1`. If there are **no healthy individuals initially**, return `0`. ### Input - A 2D integer array `grid` of size `m x n`. ### Output - An integer: the minimum minutes to infect everyone, or `-1` if impossible. ### Constraints - `1 ≤ m, n ≤ 200` - `grid[i][j] ∈ {0,1,2}` ### Notes (as in the interview) - You may be asked to write your own test cases (including edge cases) to validate your solution.

Quick Answer: This question evaluates a candidate's ability to reason about grid-based state propagation, algorithmic problem solving, and performance considerations when managing multi-step updates across a 2D matrix.

You are given an m x n grid representing the state of individuals in a lab. A cell with 0 is empty, 1 contains a healthy individual, and 2 contains an infected individual. Every minute, any healthy individual that is 4-directionally adjacent to an infected individual becomes infected. Return the minimum number of minutes needed until no healthy individuals remain. If it is impossible to infect all healthy individuals, return -1. If there are no healthy individuals initially, return 0.

Constraints

  • 1 <= m, n <= 200
  • grid[i][j] is one of {0, 1, 2}

Examples

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

Expected Output: 4

Explanation: The infection spreads outward from the top-left cell. The last healthy individual becomes infected after 4 minutes.

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

Expected Output: -1

Explanation: The healthy individual at the bottom-left is separated by empty cells and can never be infected.

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

Expected Output: 0

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

Input: ([[1]],)

Expected Output: -1

Explanation: There is one healthy individual but no infected individual to start the spread.

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

Expected Output: 2

Explanation: The infection spreads simultaneously from both infected corners, so all healthy individuals are infected in 2 minutes.

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

Expected Output: 4

Explanation: In a single row, the infection moves one cell to the right each minute.

Hints

  1. All initially infected individuals should start spreading the infection at the same time.
  2. Think of each minute as one level of a breadth-first search starting from all infected cells.
Last updated: Jun 21, 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

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)