LeetCode Problem

How to Solve Game of Life

The Game of Life requires computing the next state for every cell in a 2D grid simultaneously, based on eight neighbors. A naive approach may overwrite states early, causing incorrect transitions, so marking or in-place encoding is key. GhostInterview guides through handling neighbor counting, board updates, and simultaneous rule application without losing current cell information.

GhostInterview Help

Need help with Game of Life without spending extra time grinding it?

GhostInterview can read Game of Life from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #289Array plus MatrixReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array plus Matrix
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The Game of Life requires computing the next state for every cell in a 2D grid simultaneously, based on eight neighbors. A naive approach may overwrite states early, causing incorrect transitions, so marking or in-place encoding is key. GhostInterview guides through handling neighbor counting, board updates, and simultaneous rule application without losing current cell information.

Problem Statement

The Game of Life is a cellular automaton invented by John Horton Conway in 1970. You are given an m x n grid where each cell is either live (1) or dead (0). Each cell interacts with its eight neighbors horizontally, vertically, and diagonally, following rules that determine whether it survives, dies, or becomes alive in the next state.

Your task is to compute the next state of the entire grid simultaneously, applying the rules to every cell without changing intermediate values. Births and deaths occur simultaneously, so careful handling of updates is required to avoid premature state changes and ensure correctness across the matrix.

Examples

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 details omitted.

Example 2

Input: board = [[1,1],[1,0]]

Output: [[1,1],[1,1]]

Example details omitted.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 25
  • board[i][j] is 0 or 1.

Solution Approach

Count live neighbors efficiently

Traverse each cell and count its live neighbors by checking all eight directions. Use nested loops with bounds checking to avoid index errors while maintaining the matrix structure.

Use in-place state encoding

Mark cells with temporary values to indicate transitions (e.g., 2 for live-to-dead, 3 for dead-to-live). This preserves the original state while allowing simultaneous updates in one pass over the board.

Finalize board updates

After marking all transitions, traverse the board again to convert temporary markers to final states (1 for live, 0 for dead). This two-pass method ensures simultaneous application of Game of Life rules across the matrix.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(mn) for both passes over the board, as each cell is visited a constant number of times to check neighbors. Space complexity can be O(1) if using in-place encoding, or O(mn) if creating a separate matrix for the next state.

What Interviewers Usually Probe

  • Are you handling neighbor counts without modifying the board prematurely?
  • Can you optimize the solution to avoid extra space while maintaining simultaneous updates?
  • Do you understand why marking states in-place prevents incorrect state transitions?

Common Pitfalls or Variants

Common pitfalls

  • Updating cells in a single pass without markers causes neighbor counts to be incorrect.
  • Forgetting to check all eight neighbors, leading to off-by-one errors on edges.
  • Using extra space unnecessarily when in-place marking is possible.

Follow-up variants

  • Implement the Game of Life on an infinite grid where new cells can appear dynamically.
  • Compute k steps of the Game of Life efficiently without recomputing the entire board each time.
  • Optimize neighbor checks using prefix sums or bit manipulation for larger matrices.

How GhostInterview Helps

  • GhostInterview highlights the matrix plus array pattern and guides you to count neighbors correctly.
  • It explains in-place marking strategies to handle simultaneous updates without errors.
  • The solver identifies common edge-case mistakes and ensures your Game of Life solution passes all test scenarios.

Topic Pages

FAQ

What is the main challenge in solving the Game of Life?

The key challenge is updating all cells simultaneously without corrupting neighbor information, which requires careful state handling.

Can I solve Game of Life without extra space?

Yes, by using in-place markers to indicate transitional states, you can compute the next board state without additional arrays.

How do I count neighbors efficiently in a matrix?

Check all eight directions for each cell using loops with bounds checking to prevent index errors, ensuring accurate live neighbor counts.

What are common mistakes in this Array plus Matrix pattern?

Prematurely updating cells, missing diagonal neighbors, or mismanaging temporary markers are frequent pitfalls in Game of Life implementations.

Is there a way to simulate multiple steps of Game of Life efficiently?

Yes, by iteratively applying the in-place update strategy or using memoization techniques for repeated patterns, you can simulate multiple steps efficiently.

GhostInterview Solver

Need direct help with Game of Life instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Game of Life from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.