Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

994. Rotting Oranges

Medium55.4% Acceptance
ArrayBreadth-First SearchMatrix
Asked by:
A
Amazon
ProblemSolutions (4)VideosCompanies (4)Notes

Problem Statement

You are given an m x n grid where each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

Example 1:

Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

Example 2:

Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:

Input: grid = [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 10
  • grid[i][j] is 0, 1, or 2.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
M
Microsoft
V
VMware
L
LinkedIn

Approach

The key idea behind #994 Rotting Oranges is to simulate how rot spreads across the grid over time. Each rotten orange can infect its adjacent fresh oranges (up, down, left, right) every minute. This spreading behavior naturally fits a multi-source Breadth-First Search (BFS) approach.

Start by scanning the matrix to place all initially rotten oranges into a queue. These act as the starting points of infection. During each BFS level (representing one minute), process the current rotten oranges and convert any adjacent fresh oranges into rotten ones, adding them to the queue for the next round.

Track the number of fresh oranges remaining and the time elapsed as BFS progresses. If BFS finishes and fresh oranges still exist, it means some were unreachable. The algorithm visits each cell at most once, leading to a time complexity of O(m × n) and space complexity of O(m × n) for the queue and grid traversal.

Complexity

ApproachTime ComplexitySpace Complexity
Multi-source BFS (grid traversal)O(m × n)O(m × n)

Video Solution Available

take U forward

View all video solutions

Solutions (4)

Breadth-First Search (BFS)

This approach uses BFS to simulate the rotting process of the oranges. We start with all initially rotten oranges and spread the rot to the 4-adjacent fresh oranges, time step by time step. We use a queue to implement BFS, where each element in the queue represents a rotten orange at a specific time. The key idea is that in one minute, all current rotten oranges will rot their 4-directionally adjacent fresh oranges.

Time Complexity: O(m * n), where m and n are the number of rows and columns in the grid. Each cell is processed at most once.
Space Complexity: O(m * n) for the queue, in the worst case.

PythonJavaScript
1from collections import deque
2
3def orangesRotting(grid):
4    rows, cols = len(grid), len(grid[0])
5    queue = deque()
6    fresh_count = 0
7    time_elapsed = 0
8    
9    for r in range(rows):
10        for c in range(cols):
11            if grid[r][c] == 2:
12                queue.append((r, c, 0))
13            elif grid[r][c] == 1:
14                fresh_count += 1
15    
16    while queue and fresh_count > 0:
17        r, c, minutes = queue.popleft()
18        for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
19            nr, nc = r + dr, c + dc
20            if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1:
21                grid[nr][nc] = 2
22                fresh_count -= 1
23                queue.append((nr, nc, minutes + 1))
24                time_elapsed = minutes + 1
25    
26    return time_elapsed if fresh_count == 0 else -1

Explanation

We initialize a queue with the position of all rotten oranges (value 2) and their corresponding time, which is 0 initially. We also keep a count of fresh oranges. For each iteration, we process rotten oranges, spread the rot, and keep track of the time taken for each step. If at the end there are still fresh oranges left, we return -1.

DFS with memoization

Instead of using BFS, this approach converts the problem to a depth-first search with memoization. By examining from each fresh orange, one can determine its minimum time to rot by considering all possible paths of infection.

Time Complexity: O(m * n), as each cell is processed recursively.
Space Complexity: O(m * n) because of the memoization table and the call stack in recursion.

JavaC#
1public int orangesRotting(int[][] grid) {




Video Solutions

Watch expert explanations and walkthroughs

G-10. Rotten Oranges | C++ | Java

take U forward
22:30413,032 views

Asked By Companies

4 companies
A
Amazon
M
Microsoft
V
VMware
L
LinkedIn

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Surrounded RegionsMedium
Number of IslandsMedium
Alien DictionaryHard
Walls and GatesMedium
More similar problems

Related Topics

ArrayBreadth-First SearchMatrix

Problem Stats

Acceptance Rate55.4%
DifficultyMedium
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Rotting Oranges asked in FAANG interviews?

Yes, Rotting Oranges is a common interview problem because it tests grid traversal, BFS concepts, and simulation logic. Variations of multi-source BFS problems frequently appear in technical interviews at top tech companies.

What data structure is best for Rotting Oranges?

A queue is the most suitable data structure because the problem models a level-by-level spreading process. Using a queue allows BFS traversal so that all oranges infected at the same time are processed together.

What is the optimal approach for Rotting Oranges?

The optimal approach uses multi-source Breadth-First Search (BFS). All initially rotten oranges are inserted into a queue and spread the rot level by level to adjacent fresh oranges. Each BFS level represents one minute of time.

Why is BFS preferred over DFS for Rotting Oranges?

BFS is preferred because it processes nodes level by level, which naturally represents the passage of time in minutes. DFS does not guarantee this layer-by-layer progression, making it harder to accurately track the minimum time required.

2
int
rows
=
grid
.
length
;
3
int
cols
=
grid
[
0
]
.
length
;
4
int
[
]
[
]
memo
=
new
int
[
rows
]
[
cols
]
;
5
int
result
=
0
,
freshCount
=
0
;
6
7
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
8
for
(
int
j
=
0
;
j
<
cols
;
j
++
)
{
9
if
(
grid
[
i
]
[
j
]
==
1
)
{
10
freshCount
++
;
11
int
timeToRot
=
dfs
(
grid
,
memo
,
i
,
j
)
;
12
if
(
timeToRot
==
Integer
.
MAX_VALUE
)
return
-
1
;
13
result
=
Math
.
max
(
result
,
timeToRot
)
;
14
}
15
}
16
}
17
return
freshCount
==
0
?
result
:
-
1
;
18
}
19
20
private
int
dfs
(
int
[
]
[
]
grid
,
int
[
]
[
]
memo
,
int
r
,
int
c
)
{
21
if
(
r
<
0
||
c
<
0
||
r
>=
grid
.
length
||
c
>=
grid
[
0
]
.
length
||
grid
[
r
]
[
c
]
==
0
)
return
Integer
.
MAX_VALUE
;
22
if
(
grid
[
r
]
[
c
]
==
2
)
return
0
;
23
if
(
memo
[
r
]
[
c
]
!=
0
)
return
memo
[
r
]
[
c
]
;
24
25
grid
[
r
]
[
c
]
=
0
;
// Mark as visited
26
int
minTime
=
Integer
.
MAX_VALUE
;
27
int
[
]
dr
=
{
0
,
0
,
-
1
,
1
}
;
28
int
[
]
dc
=
{
1
,
-
1
,
0
,
0
}
;
29
30
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
31
minTime
=
Math
.
min
(
minTime
,
dfs
(
grid
,
memo
,
r
+
dr
[
i
]
,
c
+
dc
[
i
]
)
)
;
32
}
33
grid
[
r
]
[
c
]
=
1
;
34
minTime
=
minTime
==
Integer
.
MAX_VALUE
?
Integer
.
MAX_VALUE
:
minTime
+
1
;
35
memo
[
r
]
[
c
]
=
minTime
;
36
37
return
minTime
;
38
}

Explanation

This solution uses depth-first search (DFS) and memoization to find the shortest path from a fresh orange to a rotten one. Each fresh orange recursively checks all possible paths to rotten oranges, updating the minimum time via memoization.