289. Game of Life
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to simulate one step of Conway's Game of Life on a given m x n grid. The core challenge lies in the simultaneous update rule combined with the in-place modification requirement (O(1) extra space). If we update a cell, its new state will affect the neighbor count for subsequent cells in the same pass, which is incorrect. The state of all cells in the next generation must be based on the current generation's state.
Solution
Game of Life in Python
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
# Directions for 8 neighbors
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
# First pass: Mark cells with temporary states
# We use extra states to store transitions without affecting neighbor calculations:
# State 0: dead (original) -> dead (next)
# State 1: live (original) -> live (next)
# State 2: live (original) -> dead (next)
# State 3: dead (original) -> live (next)
for r in range(m):
for c in range(n):
live_neighbors = 0
# Count live neighbors based on their original state
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n:
# The original state of a neighbor is live if its value is 1 (live->live) or 2 (live->dead).
if board[nr][nc] == 1 or board[nr][nc] == 2:
live_neighbors += 1
# Apply Game of Life rules based on the cell's original state
if board[r][c] == 1: # Cell is originally live
if live_neighbors < 2 or live_neighbors > 3:
# Mark to be changed from live to dead
board[r][c] = 2
else: # Cell is originally dead (board[r][c] == 0)
if live_neighbors == 3:
# Mark to be changed from dead to live
board[r][c] = 3
# Second pass: Finalize the board states
for r in range(m):
for c in range(n):
# State 2 (live -> dead) becomes 0.
# State 3 (dead -> live) becomes 1.
# States 0 and 1 remain unchanged by this operation.
board[r][c] %= 2
Game of Life in Java
class Solution {
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
// Directions for 8 neighbors
int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] dc = {-1, 0, 1, -1, 1, -1, 0, 1};
// First pass: Mark cells with temporary states
// State 0: dead (original) -> dead (next)
// State 1: live (original) -> live (next)
// State 2: live (original) -> dead (next)
// State 3: dead (original) -> live (next)
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
int liveNeighbors = 0;
// Count live neighbors based on their original state
for (int i = 0; i < 8; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
// Original state was live if value is 1 or 2
if (board[nr][nc] == 1 || board[nr][nc] == 2) {
liveNeighbors++;
}
}
}
// Apply rules based on the cell's original state
if (board[r][c] == 1) { // Originally live
if (liveNeighbors < 2 || liveNeighbors > 3) {
board[r][c] = 2; // Mark as: live -> dead
}
} else { // Originally dead (board[r][c] == 0)
if (liveNeighbors == 3) {
board[r][c] = 3; // Mark as: dead -> live
}
}
}
}
// Second pass: Finalize the board states
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
// 2 becomes 0 (dead), 3 becomes 1 (live).
// 0 and 1 remain unchanged by this operation.
board[r][c] %= 2;
}
}
}
}
Game of Life in C++
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size();
int n = board[0].size();
// Directions for 8 neighbors
int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};
// First pass: Mark cells with temporary states
// State 0: dead (original) -> dead (next)
// State 1: live (original) -> live (next)
// State 2: live (original) -> dead (next)
// State 3: dead (original) -> live (next)
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
int live_neighbors = 0;
// Count live neighbors based on their original state
for (int i = 0; i < 8; ++i) {
int nr = r + dr[i];
int nc = c + dc[i];
if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
// Original state was live if value is 1 or 2
if (board[nr][nc] == 1 || board[nr][nc] == 2) {
live_neighbors++;
}
}
}
// Apply rules based on the cell's original state
if (board[r][c] == 1) { // Originally live
if (live_neighbors < 2 || live_neighbors > 3) {
board[r][c] = 2; // Mark as: live -> dead
}
} else { // Originally dead (board[r][c] == 0)
if (live_neighbors == 3) {
board[r][c] = 3; // Mark as: dead -> live
}
}
}
}
// Second pass: Finalize the board states
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
// 2 becomes 0 (dead), 3 becomes 1 (live).
// 0 and 1 remain unchanged by this operation.
board[r][c] %= 2;
}
}
}
};
Game of Life in TypeScript
/**
Do not return anything, modify board in-place instead.
*/
function gameOfLife(board: number[][]): void {
const m = board.length;
const n = board[0].length;
// Directions for 8 neighbors
const directions = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
// First pass: Mark cells with temporary states
// State 0: dead (original) -> dead (next)
// State 1: live (original) -> live (next)
// State 2: live (original) -> dead (next)
// State 3: dead (original) -> live (next)
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
let liveNeighbors = 0;
// Count live neighbors based on their original state
for (const [dr, dc] of directions) {
const nr = r + dr;
const nc = c + dc;
if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
// Original state was live if value is 1 or 2
if (board[nr][nc] === 1 || board[nr][nc] === 2) {
liveNeighbors++;
}
}
}
// Apply rules based on the cell's original state
if (board[r][c] === 1) { // Originally live
if (liveNeighbors < 2 || liveNeighbors > 3) {
board[r][c] = 2; // Mark as: live -> dead
}
} else { // Originally dead (board[r][c] === 0)
if (liveNeighbors === 3) {
board[r][c] = 3; // Mark as: dead -> live
}
}
}
}
// Second pass: Finalize the board states
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
// 2 becomes 0 (dead), 3 becomes 1 (live).
// 0 and 1 remain unchanged by this operation.
board[r][c] %= 2;
}
}
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.