Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Rotting oranges

Python data engineering interview problem. Difficulty: intermediate. Pattern: Graphs. About 18 minutes. Part of the Pro drill bank.

Count the minutes until every fresh orange in a grid has rotted, or report that some never do. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement rotting_oranges(grid: list[list[int]]) -> int. Each cell is 0 (empty), 1 (fresh orange) or 2 (rotten orange). Every minute, each fresh orange that touches a rotten orange up, down, left or right becomes rotten. Return the number of minutes until no fresh orange is left. Return 0 if there was no fresh orange to begin with, and -1 if some fresh orange can never rot. The grid must not be modified.

Requirements

  • Spread is simultaneous, in four directions only.

Constraints

  • Grid up to 100 x 100.
  • Cells are 0, 1 or 2.
  • Do not modify the input grid.

Examples

Input: rotting_oranges([[2, 1, 1, 2]]) Output: 1 Both rotten oranges spread at the same time, so the two fresh ones rot after a single minute.

Topics: lakebench, python, grid, multi-source, shortest time.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Rotting oranges

Interview-style drill: Count the minutes until every fresh orange in a grid has rotted, or report that some never do.

Implement `rotting_oranges(grid: list[list[int]]) -> int`. Each cell is `0` (empty), `1` (fresh orange) or `2` (rotten orange). Every minute, each fresh orange that touches a rotten orange up, down, left or right becomes rotten. Return the number of minutes until no fresh orange is left. Return `0` if there was no fresh orange to begin with, and `-1` if some fresh orange can never rot. The grid must not be modified.