Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
The next state of the board is determined by applying the above rules simultaneously to every cell in the current state of the m x n grid board. In this process, births and deaths occur simultaneously.
Given the current state of the board, update the board to reflect its next state.
Note that you do not need to return anything.
Example 1:
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Example 2:
Input: board = [[1,1],[1,0]] Output: [[1,1],[1,1]]
Constraints:
m == board.lengthn == board[i].length1 <= m, n <= 25board[i][j] is 0 or 1.Follow up:
Problem summary: According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): Any live cell with fewer than two live neighbors dies as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by over-population. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. The next state of the board is determined by applying the above rules simultaneously to every
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
[[1,1],[1,0]]
set-matrix-zeroes)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #289: Game of Life
class Solution {
public void gameOfLife(int[][] board) {
int m = board.length, n = board[0].length;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int live = -board[i][j];
for (int x = i - 1; x <= i + 1; ++x) {
for (int y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] > 0) {
++live;
}
}
}
if (board[i][j] == 1 && (live < 2 || live > 3)) {
board[i][j] = 2;
}
if (board[i][j] == 0 && live == 3) {
board[i][j] = -1;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 2) {
board[i][j] = 0;
} else if (board[i][j] == -1) {
board[i][j] = 1;
}
}
}
}
}
// Accepted solution for LeetCode #289: Game of Life
func gameOfLife(board [][]int) {
m, n := len(board), len(board[0])
for i := 0; i < m; i++ {
for j, v := range board[i] {
live := -v
for x := i - 1; x <= i+1; x++ {
for y := j - 1; y <= j+1; y++ {
if x >= 0 && x < m && y >= 0 && y < n && board[x][y] > 0 {
live++
}
}
}
if v == 1 && (live < 2 || live > 3) {
board[i][j] = 2
}
if v == 0 && live == 3 {
board[i][j] = -1
}
}
}
for i := 0; i < m; i++ {
for j, v := range board[i] {
if v == 2 {
board[i][j] = 0
}
if v == -1 {
board[i][j] = 1
}
}
}
}
# Accepted solution for LeetCode #289: Game of Life
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
m, n = len(board), len(board[0])
for i in range(m):
for j in range(n):
live = -board[i][j]
for x in range(i - 1, i + 2):
for y in range(j - 1, j + 2):
if 0 <= x < m and 0 <= y < n and board[x][y] > 0:
live += 1
if board[i][j] and (live < 2 or live > 3):
board[i][j] = 2
if board[i][j] == 0 and live == 3:
board[i][j] = -1
for i in range(m):
for j in range(n):
if board[i][j] == 2:
board[i][j] = 0
elif board[i][j] == -1:
board[i][j] = 1
// Accepted solution for LeetCode #289: Game of Life
const DIR: [(i32, i32); 8] = [
(-1, 0),
(1, 0),
(0, -1),
(0, 1),
(-1, -1),
(-1, 1),
(1, -1),
(1, 1),
];
impl Solution {
#[allow(dead_code)]
pub fn game_of_life(board: &mut Vec<Vec<i32>>) {
let n = board.len();
let m = board[0].len();
let mut weight_vec: Vec<Vec<i32>> = vec![vec![0; m]; n];
// Initialize the weight vector
for i in 0..n {
for j in 0..m {
if board[i][j] == 0 {
continue;
}
for (dx, dy) in DIR {
let x = (i as i32) + dx;
let y = (j as i32) + dy;
if Self::check_bounds(x, y, n as i32, m as i32) {
weight_vec[x as usize][y as usize] += 1;
}
}
}
}
// Update the board
for i in 0..n {
for j in 0..m {
if weight_vec[i][j] < 2 {
board[i][j] = 0;
} else if weight_vec[i][j] <= 3 {
if board[i][j] == 0 && weight_vec[i][j] == 3 {
board[i][j] = 1;
}
} else {
board[i][j] = 0;
}
}
}
}
#[allow(dead_code)]
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
i >= 0 && i < n && j >= 0 && j < m
}
}
// Accepted solution for LeetCode #289: Game of Life
/**
Do not return anything, modify board in-place instead.
*/
function gameOfLife(board: number[][]): void {
const m = board.length;
const n = board[0].length;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
let live = -board[i][j];
for (let x = i - 1; x <= i + 1; ++x) {
for (let y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] > 0) {
++live;
}
}
}
if (board[i][j] === 1 && (live < 2 || live > 3)) {
board[i][j] = 2;
}
if (board[i][j] === 0 && live === 3) {
board[i][j] = -1;
}
}
}
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (board[i][j] === 2) {
board[i][j] = 0;
}
if (board[i][j] === -1) {
board[i][j] = 1;
}
}
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.