This question evaluates a candidate's ability to design space-efficient, I/O-aware algorithms and streaming or external-memory techniques for processing extremely large binary grids, with attention to neighbor counting and boundary handling in cellular automata.
You are given starter code for computing one step of a Game of Life–style cellular automaton on a binary grid, but the naive implementation loads the entire matrix into memory.
The input grid can be as large as 1,000,000 × 1,000,000, so it cannot fit in RAM.
Assume the grid is stored on disk (e.g., one row at a time). You are provided helper functions similar to:
readRow(i) -> array<int>
returns row
i
(0/1 values)
writeRow(i, row)
writes the computed next-state row
i
Modify/implement the update so that it can process the full grid without OOM, using limited memory (e.g., O(W) or O(3W) where W is the number of columns).
Be careful about:
Return/produce the next grid on disk.