My first instinct was regular BFS from a single source and I had to catch myself because there can be multiple rotten oranges at the start.
Model the grid as a graph and use multi-source BFS starting from all initially rotten oranges simultaneously. Track the number of fresh oranges and the time elapsed; if any fresh oranges remain after BFS, return -1, otherwise return the time.
Pro tip: Clarify edge cases upfront (e.g., no fresh oranges, no rotten oranges, unreachable fresh oranges) and mention that BFS is optimal because rot spreads uniformly in all directions at the same rate.
Restate the problem to ensure clarity, and identify edge cases such as empty grid, no fresh oranges, no rotten oranges, or fresh oranges that cannot be reached.
Recognize that this is a multi-source BFS problem because rot spreads simultaneously from all rotten oranges at the same rate. BFS guarantees the minimum time.
Use a queue to store coordinates of all initially rotten oranges, and keep a count of fresh oranges. Optionally, use a visited set or modify the grid in-place to mark rotten oranges.
Process the queue in layers, where each layer represents one minute. For each rotten orange, check its four neighbors; if a neighbor is fresh, mark it rotten, decrement the fresh count, and enqueue it. Increment time after each layer.
After BFS, if fresh count is zero, return the elapsed time; otherwise, return -1. Discuss time and space complexity: O(m*n) time and O(m*n) space in the worst case.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.