Game of Life - Problem
Conway's Game of Life is a fascinating cellular automaton that simulates the evolution of life on a grid! ๐Ÿงฌ

You're given an m ร— n grid where each cell is either alive (represented by 1) or dead (represented by 0). Each cell interacts with its eight neighbors (horizontally, vertically, and diagonally adjacent cells) according to these four simple rules:

๐Ÿ”น Under-population: Any live cell with fewer than 2 live neighbors dies
๐Ÿ”น Survival: Any live cell with 2 or 3 live neighbors survives to the next generation
๐Ÿ”น Over-population: Any live cell with more than 3 live neighbors dies
๐Ÿ”น Reproduction: Any dead cell with exactly 3 live neighbors becomes alive

Important: All changes happen simultaneously - you must calculate the next state based on the current state, not on partially updated cells. Your task is to update the board in-place to reflect the next generation.

Input & Output

example_1.py โ€” Basic Evolution
$ 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]]
๐Ÿ’ก Note: The center forms a 'blinker' pattern that oscillates. Cell (1,1) dies from under-population (only 1 neighbor), while cells with exactly 3 neighbors come to life through reproduction.
example_2.py โ€” Still Life
$ Input: board = [[1,1],[1,1]]
โ€บ Output: [[1,1],[1,1]]
๐Ÿ’ก Note: This is a 'block' - a still life pattern. Each cell has exactly 2 or 3 live neighbors, so all cells survive to the next generation unchanged.
example_3.py โ€” Edge Case
$ Input: board = [[1]]
โ€บ Output: [[0]]
๐Ÿ’ก Note: A single live cell has 0 neighbors, so it dies from under-population. Edge case showing how isolated cells cannot survive.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 โ‰ค m, n โ‰ค 25
  • board[i][j] is 0 or 1
  • Follow-up: Can you solve it in-place with O(1) extra memory?

Visualization

Tap to expand
Game of Life: From Chaos to OrderGeneration 0 (Initial)EvolutionGeneration 1 (Next)Rules Applied:Under-population: < 2 neighbors โ†’ diesSurvival: 2-3 neighbors โ†’ livesOver-population: > 3 neighbors โ†’ diesReproduction: exactly 3 neighbors โ†’ birth๐Ÿ’ก Key insight: Count neighbors in original state, apply rules simultaneously3 neighbors0โ†’1 (birth)
Understanding the Visualization
1
Survey the Population
Count how many living neighbors each cell has in all 8 directions
2
Apply Natural Laws
Determine fate based on Game of Life rules: loneliness, survival, overcrowding, or reproduction
3
Simultaneous Evolution
All changes happen at once - like a synchronized biological process
4
Next Generation
The board now represents the evolved state of the cellular automaton
Key Takeaway
๐ŸŽฏ Key Insight: The magic happens when we realize we can encode both current and future states in the same memory location using bit manipulation, achieving true in-place evolution!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen