← Questions

Rotting oranges: time for an infection to cover a grid

0
AirbnbCodingLast asked Mar 2026

A coding interview question asked in Airbnb's coding round, last asked Mar 2026. It tests grids, bfs and simulation.

A grid has empty cells (0), healthy cells (1) and infected cells (2). Every minute, each healthy cell next to an infected one (up, down, left or right) becomes infected, all at the same time. Return the minutes until no healthy cell is left, or -1 if some healthy cell can never be reached. Then extend it so infection also spreads diagonally.

Part 1: four directions

Unless you agree on a different interface with the interviewer, assume:

  • minutes_to_infect(grid: list[list[int]]) -> int: 0 if there are no healthy cells to begin with.

Part 2: eight directions

  • minutes_to_infect(grid, diagonal=True): each cell has up to eight neighbors.

Example

grid = [[1, 1, 1],
        [1, 1, 1],
        [1, 1, 2]]
four directions  ->  4      # the top-left corner is 4 steps away
eight directions ->  2

grid = [[2, 1, 1],
        [0, 0, 1],
        [1, 0, 1]]
four directions  ->  -1     # the bottom-left cell is walled off
eight directions ->  -1     # its only neighbors are empty cells

Constraints

  • 1 <= rows, cols <= 300
  • There may be zero, one or many infected cells at the start. Empty cells block spread.

Clarifying questions

  • What if there are no healthy cells? No infected cells?
  • Do empty cells block the spread?
  • Is the grid allowed to be modified?

What a strong answer covers

  • Multi-source BFS: start the queue with every infected cell, process one layer per minute, count healthy cells left.
  • Counting layers, not nodes, and not adding a final minute when the last layer infects nothing.
  • O(rows × cols) time and space.
  • Part 2 changes only the list of directions; the rest is the same.
  • Tests: no healthy cells, no infected cells, unreachable cells, several sources.

Follow-ups

  • Return the minute each cell was infected.
  • Some cells are immune for the first t minutes.
  • The grid is huge and sparse: store only infected and healthy coordinates.

Where it's asked

  • Airbnb · Coding · Last asked Mar 202645 min · Live coding, one or two rounds, from the same pool as the screen

What it tests

grids · bfs · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding