FastPrepMinimum Time to Rot All Oranges
Problems/Minimum Time to Rot All Oranges

Minimum Time to Rot All Oranges

Microsoft logoMicrosoft● MediumFULLTIMEINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft onsite interview. The report names the well-known rotting oranges task without restating the rules of the grid. Our version is about 93% the same as the reported question.

You are given a rectangular grid where each cell contains one of these values:

  • 0: empty;
  • 1: a fresh orange;
  • 2: a rotten orange.

Every minute, each fresh orange that is orthogonally adjacent to a rotten orange becomes rotten. Rotting within the same minute happens simultaneously.

Return the minimum number of minutes until no fresh orange remains. Return -1 if some fresh orange can never rot. If there is no fresh orange initially, return 0.

Function

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

Examples

Example 1

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

The infection frontier reaches the final fresh orange after four minute layers.

Example 2

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

The fresh orange in the lower-left corner is separated from every rotten orange.

Constraints

  • 1 <= grid.length, grid[row].length <= 500
  • Every row has the same length.
  • grid[row][column] is 0, 1, or 2.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Number of IslandsFleetx · ONSITE INTERVIEW
  • Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
  • Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Rotting OrangesOtter.ai · ONSITE INTERVIEWOtter.ai interview questions
  • Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions

Interview reports that mention this question

  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int orangesRotting(int[][] grid) {
        // Write your code here.
    }
}
Checking account…