Game of Life
On this page
Problem statement
A grid of cells, each alive (1) or dead (0), moves forward one generation. Every cell looks at its eight neighbours (horizontal, vertical, diagonal). A live cell with 2 or 3 live neighbours stays alive; any other live cell dies. A dead cell with exactly 3 live neighbours comes alive. All cells change at the same moment. Update the grid in place.
Conway's Game of Life is a classic grid-simulation question. The rules are easy; the interview is about the simultaneous update: you must compute the next state from the old one while overwriting the same grid. Original problem: LeetCode 289.
Example

Input: Output:
0 1 0 0 0 1 1 0
0 1 1 0 0 1 1 0
0 0 1 0 0 1 1 0
0 0 0 0 0 0 0 0
Input: [[0,0,0],[1,1,1],[0,0,0]] # a "blinker"
Output: [[0,1,0],[0,1,0],[0,1,0]] # flips between horizontal and vertical
Constraints (assumed)
- 1 ≤ rows, cols ≤ 25; each cell is 0 or 1.
- Cells outside the grid count as dead.
What this question tests
- Seeing that an in-place update breaks the simultaneous rule, and fixing it.
- Encoding two states in one cell (bit tricks or extra marker values) to get O(1) extra space.
- Clean neighbour loops with boundary checks.
Questions to ask first
- Must it be in place, or can I use a copy? (Here: in place; a copy is the first step.)
- What happens at the edges? (Here: outside counts as dead. The alternative is a wrap-around board.)
- Is the board finite? (Here: yes. An infinite board is a common follow-up.)
- How many generations? (Here: one. Many generations change what's worth optimising.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.