Multi-source BFS, which I knew, but I fumbled the initialization step.
Model the grid as a graph and use multi-source BFS starting from all initially rotten oranges simultaneously. Track the time each fresh orange becomes rotten, and after BFS, check if any fresh oranges remain; if so, return -1, otherwise return the maximum time.
Pro tip: Clarify edge cases upfront (e.g., no fresh oranges, no rotten oranges, empty grid) and discuss time/space complexity (O(m*n) time and space) to demonstrate thoroughness.
Restate the problem to ensure clarity, and identify edge cases such as grids with no fresh oranges, no rotten oranges, or impossible-to-reach fresh oranges.
Recognize that this is a multi-source shortest path problem on an unweighted grid, so BFS is optimal. Explain why DFS or other approaches are less suitable.
Initialize a queue with all rotten oranges and set their time to 0. Process level by level, rotting adjacent fresh oranges and incrementing time, while tracking the number of fresh oranges remaining.
After BFS, if any fresh oranges remain unrotten, return -1; otherwise, return the maximum time recorded.
State that time and space complexity are O(m*n) where m and n are grid dimensions. Discuss potential optimizations like in-place modification or early termination.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.