289. Game of Life
Asked at Adobe
Difficulty
Medium
Time
O(m * n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given a grid of cells, where each cell is either alive (1) or dead (0). Every cell looks at its eight neighbors — the cells directly left, right, up, down, and the four diagonals.
Four rules decide the next state of each cell:
- A live cell with fewer than 2 live neighbors dies (too lonely).
- A live cell with 2 or 3 live neighbors stays alive.
- A live cell with more than 3 live neighbors dies (too crowded).
- A dead cell with exactly 3 live neighbors becomes alive.
All cells change at the same time. A cell's next state depends only on the current board, never on cells that already changed in this round. Update the board in place to show the next generation. You do not return anything — just modify the given grid.
Each cell counts its 8 neighbors
Constraints
- ●
m == board.length - ●
n == board[i].length - ●
1 <= m, n <= 25 - ●
board[i][j] is 0 or 1
Examples
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]][[0,0,0],[1,0,1],[0,1,1],[0,1,0]]board = [[1,1],[1,0]][[1,1],[1,1]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Adobe | Medium | View all Adobe questions → |
How to Think About It
Brute force: Copy the entire board, then compute each cell's next state from the copy.
Optimization: Avoid the O(mn) extra space by encoding transitions in-place with bit manipulation.
Use values 2 and -1 as temporary markers: 0 → 1 becomes 2, 1 → 0 becomes -1.
After marking all transitions, sweep the board again to replace 2 with 1 and -1 with 0.
Optimal: Single pass in-place with O(1) extra space and O(mn) time.
Optimal Approach
Iterate every cell and count its live neighbors using direction vectors for all 8 adjacent cells. Instead of copying the board, encode state transitions in-place: a cell that was alive and dies gets marked -1, and a cell that was dead and becomes alive gets marked 2. After the first pass, do a second pass to resolve markers: 2 becomes 1 and -1 becomes 0. This gives O(mn) time and O(1) extra space.
Mark first, resolve later
What Trips People Up in Real Interviews
Clarify whether you must update in-place or if extra space is allowed.
Count all 8 neighbors explicitly — don't forget diagonals.
Handle boundary cells carefully by clamping neighbor coordinates.
Walk through a 3x3 example with at least one birth and one death before coding.
Mention the encoding trick proactively — interviewers love space-optimized solutions.
Solution Code
O(m * n) timeclass Solution:
def gameOfLife(self, board):
rows, cols = len(board), len(board[0])
dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for r in range(rows):
for c in range(cols):
live = 0
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and abs(board[nr][nc]) == 1:
live += 1
if board[r][c] == 1 and (live < 2 or live > 3):
board[r][c] = -1
elif board[r][c] == 0 and live == 3:
board[r][c] = 2
for r in range(rows):
for c in range(cols):
if board[r][c] == -1:
board[r][c] = 0
elif board[r][c] == 2:
board[r][c] = 1Frequently Asked Questions
What is the Game of Life problem?
Implement Conway's Game of Life on an m x n grid where each cell is alive (1) or dead (0). Given the current state, compute the next state based on the four rules governing births, survival, deaths by loneliness, and deaths by overpopulation. The twist is to update the board in-place using encoding tricks to distinguish old from new states.
How do you solve Game of Life?
The optimal approach runs in O(m * n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Game of Life?
Game of Life is asked at Adobe. It is a medium difficulty problem.
What are common mistakes on Game of Life?
- Clarify whether you must update in-place or if extra space is allowed.
- Count all 8 neighbors explicitly — don't forget diagonals.
- Handle boundary cells carefully by clamping neighbor coordinates.
- Walk through a 3x3 example with at least one birth and one death before coding.
- Mention the encoding trick proactively — interviewers love space-optimized solutions.