Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Rotting Oranges (medium)
On this page

Problem Statement

You are given an m x n matrix in which each cell can have one of three values:

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

At each minute, any fresh orange becomes rotten if it is 4-directionally adjacent to a rotten orange.

Return the minimum number of minutes that should be passed until all the orange gets rotten. If it is impossible, return -1.

Examples

  • Input: grid =
[[2,1,0,0],
 [1,1,1,0],
 [0,1,1,1],
 [0,0,1,2]]
  • Expected Output: 3

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page