Grid Infection Spread Until Stable
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
OpenAI's May 2026 OA throws a grid infection problem at you: cells flip from healthy to infected based on neighbor count, all updates happen simultaneously each day, and you count days until nothing changes. This is a simulation that needs to track state transitions carefully. You'll scan the grid each day, mark cells that cross the threshold T, apply changes atomically, and repeat. If you blank on the synchronous update mechanic, StealthCoder will feed you the pattern in real time so you don't tank the logic.
The problem
Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Problem Grid Infection Spread Until Stable (8-direction, threshold) You are given an m×n grid grid where each cell is either infected 'X' or healthy '.'. Each day, perform a synchronous update: for every healthy cell '.', count the number of infected 'X' cells among its 8 neighbors (4-directional + 4 diagonals). If the count is ≥ T, that cell becomes infected 'X' at the end of the day. Infected cells never recover or disappear. Return the number of days until the grid becomes stable (i.e., after some day's update, no new infections occur). Notes: Updates are synchronous: changes on day d depend only on the state at the start of day d. You may assume 1 ≤ m,n ≤ 200 and 0 ≤ T ≤ 8. Examples: grid=["...", ".X.", "..."], T=1 → 1 same grid, T=2 → 0 Example Input 3 3 1....X.... Output 1 Function Description Complete solveGridInfectionSpreadUntilStable. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string array must match the expected standard output lines for the sample input. Use the limits and requirements stated in the prompt.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick here is synchronous vs asynchronous updates. Many candidates code an asynchronous version (infect a cell, then use that infected cell to spread on the same day). Wrong. You must snapshot the grid at day start, count all neighbors for every healthy cell, then apply all infections at once. This is a simulation with state copying. Use BFS or just nested loops: each iteration, scan m*n cells, count 8 neighbors for each '.', collect all cells that hit threshold T, update them together, check if anything changed. If no change, return the day count. Common pitfall: forgetting that infected cells are neighbors too, or not handling the 8-direction check correctly. The grid is at most 200x200, so brute force simulation is fine. Step through a small example by hand first to lock in the sync logic.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Grid Infection Spread Until Stable 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Grid Infection Spread Until Stable FAQ
What does 'synchronous' really mean here?+
All infections on day D are decided based on the grid state at the start of day D. You don't flip a cell and then use that flipped cell to trigger more flips the same day. Build a set of cells to infect, then apply them all at once.
How do I count the 8 neighbors correctly?+
For a cell at (i, j), check all 8 surrounding cells: (i-1,j-1), (i-1,j), (i-1,j+1), (i,j-1), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1). Count 'X' cells. If count >= T, mark that '.' for infection.
When do I stop the simulation?+
After each day's update, check if any new infections occurred. If the grid is identical to the previous day, it's stable. Return the number of days it took to reach that state.
What if T is 0 or 8?+
T=0: every '.' is neighbors with at least one 'X', so all become infected immediately (day 1). T=8: only a '.' surrounded by all 'X' flips, which is rare. T=9+: impossible with 8 neighbors, so the grid is already stable (day 0).
Do I need BFS for this problem?+
No. BFS is a red herring. This is a straightforward simulation: loop each day, scan the grid, collect cells to flip, apply them, repeat. No queue needed unless you're overcomplicating it.