Minimum Time to Spread Through a Grid
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA from September 2026 hands you a grid of 0s, 1s, and 2s and asks how many minutes until every fresh cell is active. That's the rotting oranges setup in different clothes. It's multi-source BFS, and the prompt tells you so with the phrase "every active cell" spreading at once. If you're taking this in the next day or two, you need one idea: start the queue with all the 2s together. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the pattern is short enough to own tonight.
The problem
You are given a rectangular grid whose cells contain 0, 1, or 2. A zero is empty, a one is fresh, and a two is already active. After each minute, every active cell makes each orthogonally adjacent fresh cell active. Return the minimum number of minutes until no fresh cell remains. Return -1 when this is impossible. Function minutesToSpread(grid: int[][]) → int Examples Example 1 grid = [[2,1,1],[1,1,0],[0,1,1]] return = 4 The wave reaches the lower-right fresh cell after four minutes. Example 2 grid = [[2,1,1],[0,1,1],[1,0,1]] return = -1 The isolated fresh cell in the lower-left corner is unreachable. Example 3 grid = [[0,2]] return = 0 There are no fresh cells, so no minute needs to pass. Constraints 1 <= grid.length, grid[r].length <= 200 Every row has the same length. grid[r][c] is 0, 1, or 2.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is multi-source BFS. Push every cell with a 2 into the queue at minute zero, count the fresh cells, then process the queue level by level. Each level is one minute. When you activate a fresh neighbor, decrement the fresh counter and enqueue it. At the end, if fresh is still above zero, return -1. Otherwise return the number of levels processed. The common pitfall is the off-by-one on minutes. Example 3 has no fresh cells and expects 0, so don't count a level that activated nothing. Another trap is running a separate BFS from each 2, which blows up on a 200 by 200 grid. One pass is O(rows times cols). If the level counting gets tangled under pressure, StealthCoder can give you a clean version during the live OA, but write it yourself first.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minimum Time to Spread Through a Grid cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as rotting oranges. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Time to Spread Through a Grid FAQ
What's the trick in the Amazon minimum time to spread grid problem?+
Multi-source BFS. Seed the queue with every active cell at once, then expand level by level. Each level equals one minute. Track the fresh count so you can detect unreachable cells and return -1 without scanning the whole grid again.
How do I avoid the off-by-one on minutes?+
Only increment the minute counter when a level actually activates at least one fresh cell. Or process levels and return minutes minus one if you count the final empty level. Example 3, where there are no fresh cells, must return 0.
When should I return -1?+
After the BFS finishes, if the fresh counter is still above zero, some fresh cell was never reached, usually walled off by zeros. Example 2 shows this with the lower-left corner. Return -1 in that case, otherwise return the minutes.
Is this pattern still asked at Amazon?+
It was reported in September 2026, so yes. Grid BFS with simultaneous sources is a staple. Expect variants where the spread rules change slightly, like diagonal moves or blocked cells, but the queue-by-level structure stays the same.
How do I prepare for this in 48 hours?+
Write multi-source BFS on a grid from scratch twice. Use a direction array, bounds checks, a fresh counter, and level-by-level processing. Then test the three examples by hand, especially the no-fresh-cells case and the unreachable case.