LC 994
LeetCode 994 · Medium

Rotting Oranges

Each minute, rot spreads to adjacent fresh oranges; return the minutes until none remain fresh.

BFS → · High frequency · Solve on LeetCode →

Try it

Step through the core mechanic. The simulator below runs the bfs shape this problem is built on.

Interactive · grid flood-fill
~
~
~
~
~
~
~
~
~
~
queue (front → back)
(0,0)
seed the queue with the start cell (0,0)
step 1 / 5
BFS vs DFS, same grid. Swap the frontier from a queue to a stack and breadth-first becomes depth-first. Both visit exactly the same cells; only the order differs. BFS layers outward (shortest-path / wave problems); DFS plunges down one branch (component / path problems).

The approach

Seed a queue with every rotten orange at once (multi-source BFS). Each BFS layer is one minute; the answer is the layer count. If fresh oranges survive, return −1.

AspectValue
PatternBFS
Recognise it byMulti-source BFS spreading in waves.
Time complexityO(mn)
Space complexityO(mn)
DifficultyMedium

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.