Introduction
The Rotten Oranges problem involves spreading rot across a grid.
Grid values:
0 → empty cell
1 → fresh orange
2 → rotten orange
Every minute:
rotten oranges infect adjacent fresh oranges
The task is to:
find minimum time required
to rot all oranges
If impossible:
return -1
This problem is one of the most important applications of:
Multi Source BFSThis problem helps in understanding:
BFS traversal
grid processing
shortest time spread
level-order traversal
Example
Input:grid =
[
[2,1,1],
[1,1,0],
[0,1,1]
]
Output: 4
Explanation: Minute 1: [
[2,2,1],
[2,1,0],
[0,1,1]
]
Minute 2: [
[2,2,2],
[2,2,0],
[0,1,1]
]
All oranges rot in 4 minutes.
Constraints
1 <= rows, cols <= 10^2grid[i][j] is:0, 1, or 2
Approach 1 : Brute Force Simulation
Explanation
The simplest way is:
Traverse entire grid repeatedly
Spread rot manually
Count minutes
This works but repeatedly scanning grid increases complexity.
Steps
Traverse grid.
Mark fresh oranges to rot.
Update grid after traversal.
Increase time.
Repeat until no changes occur.
Dry Run
Input:[
[2,1,1],
[1,1,0],
[0,1,1]
]
Minute 1: Adjacent oranges rot
Minute 2:
More oranges rot
Continue simulation...
Final Result: 4
Complexity Analysis
Time Complexity: O((m*n)²)Explanation:Entire grid is repeatedly scanned.
Space Complexity: O(m*n) Explanation:
Temporary storage is used.
Approach 2 : Multi Source BFS
Explanation
The optimized solution uses:
Multi Source BFSIdea:
all rotten oranges act as BFS sources
every BFS level represents:
one minute
Process:
Add all rotten oranges to queue
Spread rot level by level
Count minutes
This naturally models infection spread.
Steps
Add all rotten oranges to queue.
Count fresh oranges.
Perform BFS traversal.
Rot adjacent fresh oranges.
Reduce fresh count.
Return total minutes.
Dry Run
Initial Queue:[(0,0)]
Minute 1: (0,1)
(1,0)
Minute 2: (0,2)
(1,1)
Minute 3: (2,1)
Minute 4:
(2,2)
Final Result: 4
Multi Source BFS Code
Complexity Analysis
Time Complexity: O(m * n)Explanation:
Each cell is processed once.
Space Complexity: O(m * n) Explanation:
Queue stores grid cells.
Edge Cases
No fresh oranges
No rotten oranges
Single cell grid
Impossible infection spread
Entire grid already rotten
Why This Problem is Important
Rotten Oranges helps in understanding:
Multi Source BFS
Grid traversal
Level-order processing
Time-based spreading
Queue traversal
It is one of the most important BFS interview problems.
Real-World Applications
Multi-source BFS concepts are used in:
Virus spread simulation
Fire spread systems
Network broadcasting
Flood fill problems
Shortest time propagation systems
Common Mistakes
Forgetting all BFS sources
Incorrect minute counting
Missing fresh orange tracking
Revisiting already rotten cells
Interview Tips
Interviewers often expect:
Multi-source BFS explanation
Level-order traversal understanding
Time complexity reasoning
Always explain:
why all rotten oranges start together
why each BFS level equals one minute
how fresh orange count avoids rescanning
Related Questions
Binary Tree Level Order Traversal
Word Ladder
Open the Lock
Flood Fill
Number of Islands
Final Takeaway
The Rotten Oranges problem is a fundamental multi-source BFS problem that teaches level-order traversal and time-based grid propagation techniques. Understanding this problem builds a strong foundation for advanced graph and BFS interview problems.