
Rotting Oranges Problem Statement
You are given a grid containing oranges where each cell of the grid can contain one of the three integer values:
- 0 - representing an empty cell
- 1 - representing a fresh orange
- 2 - representing a rotten orange
Every second, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten. Your task is to find the minimum time required after which no cell contains a fresh orange. If it is impossible to rot all the fresh oranges, return -1.
Note:
1. The grid is 0-based indexed.
2. A rotten orange can affect adjacent oranges 4 directionally, i.e., Up, Down, Left, Right.
Example:
Input:
N = 3, M = 3
grid = [
[2, 1, 1],
[1, 1, 0],
[0, 1, 1]
]
Output:
4
Explanation:
After 4 seconds, all fresh oranges become rotten.
Constraints:
- 1 <= N <= 500
- 1 <= M <= 500
- 0 <= grid[i][j] <= 2
- Time Limit: 1 sec

AnswerBot
12d

Find minimum time to rot all fresh oranges adjacent to rotten oranges in a grid.
Use Breadth First Search (BFS) to simulate the rotting process
Keep track of the time taken to rot all fresh oranges
Retur...read more

Help your peers!
Add answer anonymously...
Top Infosys System Engineer Specialist interview questions & answers
Top HR questions asked in Infosys System Engineer Specialist
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app