FastPrepMinutes Until All Oranges Rot
Problems/Minutes Until All Oranges Rot

Minutes Until All Oranges Rot

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem counts the minutes until every fresh orange in a grid has rotted, when rot spreads each minute to side-by-side neighbours, or reports that some orange never rots. An Adobe Computer Scientist report gives this as the second of two coding questions in its first DSA round, only as a link to the well-known rotting-oranges problem; it states no rules, limits or examples of its own.

Our choices: the usual rules of that problem (0 empty, 1 fresh, 2 rotten; four directions; 0 when nothing is fresh and -1 when an orange is cut off), grids up to 50 x 50, and new examples.

The judged task matches the report at about 90%.

A crate is an m x n grid. Each cell is 0 (empty), 1 (a fresh orange) or 2 (a rotten orange).

Every minute, each fresh orange that shares a side (up, down, left or right) with a rotten orange becomes rotten. All of a minute's changes happen together, so an orange that rots during a minute starts spreading only in the next minute.

Return the number of minutes until no fresh orange is left. Return 0 if there is no fresh orange at the start, and -1 if some fresh orange can never rot.

Function

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

Examples

Example 1

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

Minute 1: the rotten orange in the middle rots the one above it and the one to its right. Minute 2: the top-middle orange rots both top corners, and the right-middle orange rots the bottom-right one. No fresh orange is left after 2 minutes.

Example 2

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

Both ends spread at the same time. Minute 1 rots positions 1 and 4; minute 2 rots positions 2 and 3. The answer is 2, not the 4 it would take from one end alone.

Example 3

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

The fresh orange at the top-left touches the rotten one only at a corner. Rot does not spread diagonally, so it never rots: return -1.

Constraints

  • 1 <= m, n <= 50, where m is the number of rows and n the number of columns; every row has n cells
  • each grid[i][j] is 0, 1 or 2

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int minutesUntilAllRot(int[][] grid) {
    // Write your code here.
}
Checking account…