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
4mo

Find the minimum time required to rot all fresh oranges in a grid.

  • Use Breadth First Search (BFS) to simulate the rotting process.

  • Track the time taken to rot all oranges and return the result.

  • Handle ed...read more

Help your peers!
Select
Add answer anonymously...

Jio Platforms Software Developer interview questions & answers

A Software Developer was asked 7mo agoQ. Explain the rolling hash algorithm and its applications in data structures and a...read more
A Software Developer was asked Q. Write a program to print the Fibonacci series.
A Software Developer was asked 12mo agoQ. Synchronization in Java

Popular interview questions of Software Developer

A Software Developer was asked 7mo agoQ1. Explain the rolling hash algorithm and its applications in data structures and a...read more
A Software Developer was asked Q2. Write a program to print the Fibonacci series.
A Software Developer was asked 12mo agoQ3. Synchronization in Java
Jio Platforms Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits