Rotting Orange Propagation
Updated
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.
This is a BFS task similar to Rotten Oranges, with the propagation rules above.
Function
minutesToRot(grid: int[][]) → intExamples
Example 1
grid = [[2,1],[1,1]]return = 2At minute 1 the two neighbors of the corner rot; at minute 2 the opposite corner rots.
Example 2
grid = [[2,1,0,1]]return = -1The empty cell blocks the route to the final fresh orange.
Example 3
grid = [[0,2]]return = 0No fresh orange is present, so no time is needed.
Constraints
- For this exercise, assume an empty grid is allowed. Otherwise there are
1through20rows and columns, and every row has the same length. - Every cell is one of
0,1, or2.
More Indeed Phone Screen questions
- Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Course Completion OrderPHONE SCREEN · Seen May 2025
- Recipes You Can PreparePHONE SCREEN · Seen May 2025
- Courses Shared by Every Student PairPHONE SCREEN · Seen Aug 2022
- First and Last Access Time per UserPHONE SCREEN · Seen Jul 2022
- Most Accessed Resource in Five MinutesPHONE SCREEN · Seen Jul 2022
- Resource Transition GraphPHONE SCREEN · Seen Jul 2022
- Count Disappearing Connected TilesPHONE SCREEN · Seen Jul 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Jul 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Jul 2022
Similar questions at other companies
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions