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.
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
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.