← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE coding round, got a grid BFS problem about spreading rot through oranges. Pretty classic multi-source BFS setup but the edge cases tripped me up more than I expected.

Questions Asked (1)

Q1

You're given a grid where each cell is either empty, a fresh orange, or a rotten orange. Rotten oranges spread rot to adjacent fresh oranges every minute. Return the minimum number of minutes until no fresh oranges remain, or -1 if it's not possible.

Algorithms & Data Structures
Author's notes

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.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Understand the problem and edge cases

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.

2. Choose the right algorithm

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.

3. Initialize data structures

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.

4. Perform BFS level by level

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.

5. Return the result

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.

Key Points to Mention

  • Multi-source BFS is ideal because rot spreads from multiple sources simultaneously.
  • Use a queue to process oranges in order of their rot time, ensuring minimum minutes.
  • Track the number of fresh oranges to quickly determine if all have rotted.
  • Handle edge cases: no fresh oranges (return 0), no rotten oranges (return -1 if fresh exist), unreachable fresh oranges (return -1).
  • Time complexity is O(m*n) since each cell is processed at most once; space complexity is O(m*n) for the queue.
  • Optionally, modify the grid in-place to save space, but be mindful of side effects.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.