FastPrepRotting Orange Propagation

Rotting Orange Propagation

Indeed logoIndeed● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a rectangular grid, each cell is 0 for empty, 1 for a fresh orange, or 2 for a rotten orange.

For this exercise, assume each minute every orange rotten at the start of that minute infects its fresh neighbors directly above, below, left, and right. All infections in a minute happen simultaneously. Empty cells remain empty and do not transmit rot.

Return the smallest number of minutes until no fresh orange remains. Return -1 if some fresh orange can never rot, and 0 if the input has no fresh oranges.

The interview report identifies a BFS task similar to Rotten Oranges without describing its precise variation. These propagation rules define the authored practice version.

Function

minutesToRot(grid: int[][]) → int

Examples

Example 1

grid = [[2,1],[1,1]]return = 2

At minute 1 the two neighbors of the corner rot; at minute 2 the opposite corner rots.

Example 2

grid = [[2,1,0,1]]return = -1

The empty cell blocks the route to the final fresh orange.

Example 3

grid = [[0,2]]return = 0

No fresh orange is present, so no time is needed.

Constraints

  • For this exercise, assume an empty grid is allowed. Otherwise there are 1 through 20 rows and columns, and every row has the same length.
  • Every cell is one of 0, 1, or 2.

More Indeed problems

See Indeed hiring insights
public int minutesToRot(int[][] grid) {
    // Write your code here.
    return 0;
}
grid[[2,1],[1,1]]
expected2
Checking account…