DIY: Rotting Oranges

Solve the interview question "Rotting Oranges" in this lesson.

Problem Statement

You are given an m * n grid. Each cell in the grid contains one of the three values: [0, 1, 2]. Each value represents the following:

  • 0 represents an empty cell.
  • 1 represents a fresh orange.
  • 2 represents a rotten orange.

A fresh orange, represented by a 1 in the grid, will be rotten if it is four directionally adjacentfour\ directionally\ adjacent cells to a rotten orange, represented by 2 for one minute.

You can safely assume that the cells won’t contain any other value.

You have to find the minimum number of minutes that must elapse until there are no fresh oranges left in the grid.

Let’s discuss an example below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.