Tap here to switch tabs
Problem
Ask
Submissions

Problem: Game of Life

med
30 min
Understand the Game of Life problem and its rules governing cell states in a grid. Learn to apply matrix traversal and update techniques to simulate cellular automata, preparing you to solve similar coding interview questions involving matrices and state transformation.

Statement

The Game of Life, also known simply as Life, is a cellular automaton introduced by British mathematician John Horton Conway in 1970.

We are given an m×nm \times n grid representing the board. Each cell has one of two states:

  • 1 → Alive cell

  • 0 → Dead cell

Each cell interacts with its eight neighbors (horizontal, vertical, and diagonal) according to the following rules:

  1. An alive cell with fewer than 2 alive neighbors dies → underpopulation.

  2. An alive cell with 2 or 3 alive neighbors survives → lives to the next generation.

  3. An alive cell with more than 3 alive neighbors dies → overpopulation.

  4. A dead cell with exactly 3 alive neighbors becomes an alive cell → reproduction.

The board is updated using the above rules, with all updates applied simultaneously across the board. This means the next state of the grid is determined from the current state without interference from ongoing updates.

Given the current state of the board, your task is to update the board to reflect its next state.

Note: You do not need to return anything.

Constraints:

  • m==m == board.length

  • n==n== board[i].length

  • 1m,n251 \leq m, n \leq 25

  • board[i][j] is either 0 or 1.

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Game of Life

med
30 min
Understand the Game of Life problem and its rules governing cell states in a grid. Learn to apply matrix traversal and update techniques to simulate cellular automata, preparing you to solve similar coding interview questions involving matrices and state transformation.

Statement

The Game of Life, also known simply as Life, is a cellular automaton introduced by British mathematician John Horton Conway in 1970.

We are given an m×nm \times n grid representing the board. Each cell has one of two states:

  • 1 → Alive cell

  • 0 → Dead cell

Each cell interacts with its eight neighbors (horizontal, vertical, and diagonal) according to the following rules:

  1. An alive cell with fewer than 2 alive neighbors dies → underpopulation.

  2. An alive cell with 2 or 3 alive neighbors survives → lives to the next generation.

  3. An alive cell with more than 3 alive neighbors dies → overpopulation.

  4. A dead cell with exactly 3 alive neighbors becomes an alive cell → reproduction.

The board is updated using the above rules, with all updates applied simultaneously across the board. This means the next state of the grid is determined from the current state without interference from ongoing updates.

Given the current state of the board, your task is to update the board to reflect its next state.

Note: You do not need to return anything.

Constraints:

  • m==m == board.length

  • n==n== board[i].length

  • 1m,n251 \leq m, n \leq 25

  • board[i][j] is either 0 or 1.