Rotting oranges (multi-source BFS). Compare repeated simulation with level-order BFS.
Problem
Grid of 0 (empty), 1 (fresh) and 2 (rotten). Each minute, rotten oranges rot their 4-directional fresh neighbours. Return the minutes until no fresh orange remains, or -1.
Pattern
Multi-source BFS: put every starting point in the queue at distance 0 and expand level by level; each level is one unit of time. Same idea for "distance to nearest X" problems.
Approach 1: Simulate minute by minute, scanning the whole grid (feasible, O((m·n)²))
class RottingSimulation {
static int minutes(int[][] grid) {
int[][] g = new int[grid.length][];
for (int i = 0; i < grid.length; i++) g[i] = grid[i].clone();
int minutes = 0;
while (true) {
boolean changed = false;
int[][] next = new int[g.length][];
for (int i = 0; i < g.length; i++) next[i] = g[i].clone();
for (int r = 0; r < g.length; r++)
for (int c = 0; c < g[0].length; c++)
if (g[r][c] == 1 && (rotten(g, r - 1, c) || rotten(g, r + 1, c) || rotten(g, r, c - 1) || rotten(g, r, c + 1))) {
next[r][c] = 2; changed = true;
}
if (!changed) break;
g = next;
minutes++;
}
for (int[] row : g) for (int v : row) if (v == 1) return -1;
return minutes;
}
private static boolean rotten(int[][] g, int r, int c) { return r >= 0 && r < g.length && c >= 0 && c < g[0].length && g[r][c] == 2; }
}Approach 2: Multi-source BFS (O(m·n)) — optimal
import java.util.ArrayDeque;
import java.util.Deque;
class RottingBfs {
private static final int[][] DIRS = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static int minutes(int[][] grid) {
int rows = grid.length, cols = grid[0].length, fresh = 0;
int[][] g = new int[rows][];
Deque<int[]> queue = new ArrayDeque<>();
for (int r = 0; r < rows; r++) {
g[r] = grid[r].clone();
for (int c = 0; c < cols; c++) {
if (g[r][c] == 2) queue.offer(new int[] {r, c}); // all sources at time 0
else if (g[r][c] == 1) fresh++;
}
}
int minutes = 0;
while (!queue.isEmpty() && fresh > 0) {
for (int size = queue.size(); size > 0; size--) { // one level = one minute
int[] cell = queue.poll();
for (int[] d : DIRS) {
int nr = cell[0] + d[0], nc = cell[1] + d[1];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && g[nr][nc] == 1) {
g[nr][nc] = 2;
fresh--;
queue.offer(new int[] {nr, nc});
}
}
}
minutes++;
}
return fresh == 0 ? minutes : -1;
}
}
class RottingDemo {
public static void main(String[] args) {
int[][] a = {{2, 1, 1}, {1, 1, 0}, {0, 1, 1}}, b = {{2, 1, 1}, {0, 1, 1}, {1, 0, 1}};
System.out.println(RottingSimulation.minutes(a) + " " + RottingBfs.minutes(a)); // 4 4
System.out.println(RottingSimulation.minutes(b) + " " + RottingBfs.minutes(b)); // -1 -1
}
}Feasible → optimal
Repeated full-grid simulation is O((m·n)²) in the worst case; multi-source BFS touches each cell once: O(m·n) time and space.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): distance of every cell to the nearest 0 (01 matrix).
Start BFS from all zeros at distance 0; each popped cell gives its unvisited neighbours distance + 1. O(m·n). Running a separate BFS from every 1 would be O((m·n)²).
Related (same pattern): walls and gates, or "as far from land as possible".
Both are multi-source BFS from all gates (or all land cells); the answer is the distance assigned when a cell is first reached, or the last BFS level.