Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Queue / BFS High frequency Solution Available Practice Mode

Rotting Oranges LeetCode Solution

Tags: #Array #BFS #Matrix

Approach Summary

Multi-source BFS starting from all rotten oranges simultaneously. Count BFS levels (minutes).

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Queue / BFS Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Queue / BFS pattern.

Framework: Queue / BFS • Interview Prep: Tested at FAANG • Explore all Queue / BFS problems & invariants →

How to Recognize This Pattern

  • Simultaneous spread from multiple sources
  • BFS levels = time

Complexity Analysis

Time Complexity

O(m × n)

Space Complexity

O(m × n)

Tags

Array BFS Matrix

Frequently Asked Questions

What is the optimal time and space complexity for Rotting Oranges?
The optimal solution for Rotting Oranges achieves O(m × n) runtime complexity and requires O(m × n) auxiliary space. Each cell is enqueued at most once — O(m × n) time. The queue holds at most the frontier of rotten cells — O(m × n) worst case.
How do I identify that Rotting Oranges requires the Queue / BFS pattern?
In technical interviews, look for key problem signals: Simultaneous spread from multiple sources; BFS levels = time. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Queue / BFS strategy.
What critical edge cases must be handled in Rotting Oranges?
Essential edge cases to verify include: No fresh oranges at the start — return 0; A fresh orange isolated by empty cells — return -1; A single rotten orange in a large grid — minutes equal the longest distance; Empty grid — return 0. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Rotting Oranges?
Rotting Oranges is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google.
Buy me a coffee
Daily Practice
0/0