Orange Rotting Problem Statement
Consider a grid containing oranges. Each cell in this grid can hold one of three integer values:
- Value 0: Represents an empty cell.
- Value 1: Represents a fresh orange.
- Value 2: Represents a rotten orange.
Every second, any fresh orange adjacent (in the four cardinal directions) to a rotten orange turns rotten.
The task is to determine the minimum time required for all fresh oranges to become rotten. Return -1 if it's impossible to rot all fresh oranges.
Input:
The first line contains two integers 'N' and 'M', denoting the grid's number of rows and columns. The next 'N' lines each contain 'M' space-separated integers, representing rows of the grid.
Output:
A single integer representing the minimum time after which no fresh orange remains. Return -1 if it is impossible.
Example:
Input: N = 3, M = 3 grid = [ [2,1,1], [1,1,0], [0,1,1] ] Output: 4
Explanation: At the end of 4 minutes, all oranges have rotted.
Constraints:
- 1 <= N <= 500
- 1 <= M <= 500
- 0 <= grid[i][j] <= 2
- Time Limit: 1 sec
Note:
Implement your solution without printing anything, as the printing is handled elsewhere.

AnswerBot
4mo
The task is to determine the minimum time required for all fresh oranges to become rotten in a grid.
Create a queue to store the rotten oranges and their time of rotting.
Iterate through the grid to fin...read more
Help your peers!
Add answer anonymously...
Uber Software Engineer interview questions & answers
A Software Engineer was asked Q. Given a read-only array, find the kth smallest element in an unordered array wit...read more
A Software Engineer was asked Q. Given a 2D matrix with some doors (D) and walls (W), how would you fill a distan...read more
A Software Engineer was asked Q. Boolean Matrix Transformation Challenge Given a 2-dimensional boolean matrix mat...read more
Popular interview questions of Software Engineer
A Software Engineer was asked 10mo agoQ1. Describe the architecture of Netflix.
A Software Engineer was asked Q2. Given a read-only array, find the kth smallest element in an unordered array wit...read more
A Software Engineer was asked Q3. Given a 2D matrix with some doors (D) and walls (W), how would you fill a distan...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

