Rotting Oranges
Given a grid where 0=empty, 1=fresh orange, 2=rotten orange Each minute, rotten oranges spread to adjacent fresh oranges (4-directionally) Return the minimum minutes until all oranges are rotten, or -1 if impossible Example: [[2,1,1],[1,1,0],[0,1,1]] => 4
Rot spreads one layer per minute, so I seed the queue with every rotten orange at once. BFS depth is the answer.
Examples
You write
oranges_rotting(grid: List[List[int]])
Example 1
- Input
- grid = [[2,1,1],[1,1,0],[0,1,1]]
- returns
- 4
Example 2
- Input
- grid = [[2,1,1],[0,1,1],[1,0,1]]
- returns
- -1
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsBFS & DFS
I reach for it when
- You walk a grid, a graph or a tree and visit each cell once.
- The word shortest appears and every step costs the same.
Say this before you type. Say why breadth or depth fits, and exactly when a cell counts as visited.
All 52 BFS & DFS problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.