← Questions
Game of Life, updated in place
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked May 2026. It tests matrices, simulation and in-place.
Each cell of an m x n grid is alive (1) or dead (0). All cells update at the same time from the states of their eight neighbours, by Conway's Game of Life rules:
- A live cell with fewer than two live neighbours dies.
- A live cell with two or three live neighbours lives.
- A live cell with more than three live neighbours dies.
- A dead cell with exactly three live neighbours becomes alive.
Compute the next state in place, without allocating a second grid.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
next_state(board: list[list[int]]) -> None— modifiesboard.
Example
[[0,1,0], [[0,0,0],
[0,0,1], -> [1,0,1],
[1,1,1], [0,1,1],
[0,0,0]] [0,1,0]]
[[1,1], [[1,1],
[1,0]] -> [1,1]] # the dead corner has three live neighbours
Constraints
-
1 <= m, n <= 1000. Cells outside the grid count as dead.
Clarifying questions
- Do the edges wrap around, or are cells outside the grid dead?
- Is
O(1)extra space required, or just no second full grid? - Will we run many generations in a row?
What a strong answer covers
- Why naive in-place updates are wrong: later cells would read already-updated neighbours.
- Encoding both states in each cell: keep the current state in bit 0 and write the next state into bit 1 (or use markers like 2 = "alive to dead", 3 = "dead to alive"), then a second pass shifts every cell.
O(mn)time,O(1)extra space. - Counting neighbours with boundary checks.
- Tests: a blinker oscillating, a still block, a single cell, a 1 x n grid.
Follow-ups
- The board is infinite and sparse: store live cells in a set and update only them and their neighbours.
- The board is too big for memory: process it in row bands, keeping the rows around each band.
- Run
kgenerations as fast as possible, or in parallel.
Where it's asked
- Google · First round: coding · Last asked May 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
matrices · simulation · in-place
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
