Rotten Oranges / Grid Infection BFS
Reported by candidates from Lyft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole Lyft question from August 2026 comes down to one data structure: a queue. If you've got an OA coming up, expect the grid infection problem, which is Rotting Oranges in a lab coat. Cells are 0, 1, or 2, infection spreads in four directions each minute, and you return the minutes until no fresh cells remain, or -1. It's multi-source BFS, and the queue is what makes the minute counting honest. If you blank on the setup mid-assessment, StealthCoder is the invisible safety net that reads the problem and hands you the solution.
The problem
You are given a two-dimensional grid representing cells in an infection simulation. Each cell has one of three values: 0: empty 1: fresh 2: infected Each minute, every currently infected cell simultaneously infects its 4-directional fresh neighbors. Fresh cells infected during the current minute can only infect other cells in later minutes. Return the minimum number of minutes needed until there are no fresh cells left. If some fresh cell can never be infected, return -1. If there are no fresh cells at the start, return 0. Function minimumMinutesToInfectAll(grid: int[][]) → int Examples Example 1 grid = [[2, 1, 1], [1, 1, 0], [0, 1, 1]] return = 4 The infection spreads level by level from the initial infected cell. The last fresh cell is infected after 4 minutes. Example 2 grid = [[2, 1, 1], [0, 1, 1], [1, 0, 1]] return = -1 The fresh cell in the lower-left corner is isolated by empty cells, so it can never be infected. Example 3 grid = [[0, 2]] return = 0 There are no fresh cells at the start. Constraints grid is a rectangular matrix. Each cell is 0, 1, or 2. Infection spreads only in the four orthogonal directions.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to seed the queue with every infected cell at once, not run BFS from each one separately. Count fresh cells up front. Then process the queue level by level, where each level is one minute. When you infect a neighbor, flip it to 2 so it's never queued twice, and decrement the fresh count. The classic pitfall is the off-by-one on minutes. If you increment the timer for every level, you overcount by one, because the last level infects nothing new. Only bump the timer when that level infected something, or return time minus one. Also handle zero fresh cells at the start by returning 0 immediately, like Example 3. At the end, if fresh is still above zero, return -1, like the isolated corner in Example 2. If the level-by-level loop slips away from you live, StealthCoder can cover that gap during the OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Rotten Oranges / Grid Infection BFS 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
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 Lyft's OA.
Lyft reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rotten Oranges / Grid Infection BFS FAQ
What's the trick to the Lyft grid infection problem?+
Multi-source BFS. Push all initially infected cells into the queue together, then expand one level per minute. A single BFS from one source gives wrong answers because infections spread simultaneously. Track a fresh-cell counter so you know whether everything got infected.
Why does my answer come out one minute too high?+
You're incrementing the timer on the final level, which infects nothing new. Either only increment when a level actually infects at least one fresh cell, or return the timer minus one. Test it on Example 3 and a grid with no fresh cells.
When do I return -1?+
After the BFS finishes, if the fresh counter is still above zero. That means some fresh cell was walled off by empty cells, like the lower-left corner in Example 2. Don't scan the grid again, just check the counter.
Is BFS or DFS right for this one?+
BFS. You need the minimum minutes, and BFS processes cells in order of distance from the nearest infected cell. DFS would explore deep paths and give you wrong timing unless you add messy distance bookkeeping.
How do I prepare for this in 48 hours?+
Write the multi-source BFS template from memory three times: init queue, count fresh, loop by level size, four-direction neighbors with bounds checks. Then run the three examples by hand. That covers the edge cases, no fresh cells and unreachable cells.