Rotting Oranges — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “A grid has fresh and rotten oranges. Each minute, rotten oranges infect all adjacent fresh oranges. How many minutes until all oranges are rotten?” You know this is BFS — the minute-by-minute spread from multiple sources is a textbook multi-source BFS problem. The challenge is recognizing that you need to seed the queue with all rotten oranges simultaneously, not one at a time.
The Problem
You are given an m x n integer grid where each cell is:
0— empty1— fresh orange2— rotten orange
Every minute, any fresh orange adjacent (4-directionally) to a rotten orange becomes rotten. Return the minimum number of minutes for all fresh oranges to rot, or -1 if it is impossible.
Example
Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Multi-source BFS | Do you seed the queue with ALL rotten oranges at once? |
| Layer-by-layer BFS | Do you track minutes via BFS levels? |
| Fresh count tracking | Do you count fresh oranges upfront and decrement? |
| Impossible case | Do you detect unreachable fresh oranges and return -1? |
Step 1: Clarifying Questions
- 4-directional or 8-directional? 4-directional — no diagonals.
- What if there are no fresh oranges? Return
0. - What if there are no rotten oranges but there are fresh ones? Return
-1.
Step 2: The Key Insight
Multi-source BFS: seed the queue with every rotten orange simultaneously. This models parallel spread correctly. Each BFS level corresponds to one minute. After BFS completes, if any fresh oranges remain, return -1.
---
Step 3: Algorithm
- Scan the grid. Add all rotten oranges to a queue. Count fresh oranges.
- If
fresh_count == 0, return0. - BFS level by level (each level = one minute): for each rotten orange, check 4 neighbors. If fresh, mark rotten, decrement fresh count, enqueue.
- After BFS: if
fresh_count == 0, return minutes. Otherwise-1.
Python Implementation
from collections import deque
def orangesRotting(grid: list[list[int]]) -> int:
rows, cols = len(grid), len(grid[0])
queue = deque()
fresh_count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 2:
queue.append((r, c, 0))
elif grid[r][c] == 1:
fresh_count += 1
if fresh_count == 0:
return 0
max_minutes = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while queue:
r, c, minute = queue.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1:
grid[nr][nc] = 2
fresh_count -= 1
max_minutes = max(max_minutes, minute + 1)
queue.append((nr, nc, minute + 1))
return max_minutes if fresh_count == 0 else -1
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(m × n) — each cell enqueued at most once |
| Space | O(m × n) — queue can hold all cells |
Common Interview Mistakes
-
Single-source BFS. Running BFS from each rotten orange separately models sequential spread, not simultaneous. Results are wrong for grids with multiple rotten oranges.
-
Off-by-one on minutes. Tracking minutes with a level counter can produce ±1 bugs. Storing the time in the queue entry itself is cleanest.
-
Not checking for remaining fresh oranges. BFS completing doesn’t mean all oranges are rotten. Check
fresh_count == 0.
What a Strong Candidate Sounds Like
“This is multi-source BFS. I seed the queue with all rotten oranges at once to model simultaneous spread. Each BFS level is one minute. I track fresh count upfront and decrement as oranges rot. After BFS, if any fresh remain, they’re isolated and I return -1.”
Example Interview Dialogue
Interviewer: Why multi-source BFS instead of running BFS from each rotten orange?
Candidate: Because the spread is simultaneous. If two rotten oranges are on opposite ends of the grid, they infect neighbors at the same time. Multi-source BFS models this by starting all sources at minute 0. Running BFS from each source sequentially would double-count time.
Interviewer: How do you detect impossible cases?
Candidate: I count fresh oranges before BFS. Each time a fresh orange turns rotten, I decrement. After BFS, if fresh_count > 0, those oranges were never reached — I return -1.
Related Problems to Practice
- Number of Islands — BFS/DFS grid traversal, single-source.
- 01 Matrix — Multi-source BFS to find nearest distances.
- Flood Fill — Grid DFS with implicit visited.
- Course Schedule — Graph BFS/DFS with topological sort.
Practice This in a Mock Interview
Rotting Oranges is a reliable signal for BFS fluency. The multi-source seeding is the insight that separates a good answer from a correct-but-naive one. If you can explain why single-source BFS is wrong and implement multi-source confidently, you’re ready for any BFS variant.
Start a mock interview for Rotting Oranges on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub