Skip to interview prep
Apexed

99. Rotting Oranges (Multi-Source BFS)

MediumGraphs

In a grid, 0 is an empty cell, 1 a fresh orange and 2 a rotten orange. Each minute, every fresh orange next to a rotten one (up, down, left or right) becomes rotten.

Return the number of minutes until no fresh orange remains, or -1 if some orange can never rot.

Example 1

Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

Example 2

Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1

Constraints

  • 1 ≤ rows, cols ≤ 10

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Rotting Oranges (Multi-Source BFS) — coding interview practice — Apexed