mediumGraphsAmazonFacebook
Citrus Spoilage Timer
A warehouse tracks how a mold outbreak on crate-stacked oranges spreads to all neighbors each minute.
In a grid, 0 = empty, 1 = fresh orange, 2 = rotten. Every minute, any fresh orange adjacent to a rotten one rots. Return the minutes until no fresh remain, or -1 if impossible.
Choose approach
🦠0
🍊
🍊
🍊
🍊
0
0
🍊
🍊
1/8
Narrationstep 1 of 8
Seed BFS from 1 rotten oranges. Fresh = 6.
Variables
fresh=6
rotten=1
How to think about it
- Multi-source BFS = each starting rot acts independently; BFS depth = time.
- Decrement fresh counter as neighbors rot.
- Non-zero fresh at end -> isolated -> -1.
Edge cases to catch in interview
- No fresh oranges -> 0.
- Only fresh, no rotten -> -1.
Examples
Example 1
4{"grid":[[2,1,1],[1,1,0],[0,1,1]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed