FastPrepGame of Life: In-Place Next Generation
Problems/Game of Life: In-Place Next Generation

Game of Life: In-Place Next Generation

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source explicitly names LeetCode 289 and adds an in-place-modification follow-up. The standard Game of Life rules are recovered from the canonical referenced exercise and corroborating live source family; the portable return adapter exposes the mutated board for exact testing. The judged core matches the visible source at about 95%.

Given a finite rectangular binary grid board, compute one simultaneous generation of Conway's Game of Life.

  • A live cell with two or three live neighbors survives.
  • A dead cell with exactly three live neighbors becomes live.
  • Every other cell is dead in the next generation.

Each cell has up to eight horizontal, vertical, and diagonal neighbors. Cells outside the grid are dead. Every decision must use the original generation.

Modify the supplied board in place and return it. You may encode temporary old/new state in each cell, but you must not allocate another grid proportional to the board size.

Function

gameOfLife(board: int[][]) → int[][]

Examples

Example 1

board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]return = [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

Neighbor counts come from the original four-row grid, so all births and deaths occur simultaneously.

Example 2

board = [[1,1],[1,0]]return = [[1,1],[1,1]]

The dead corner has three live neighbors and is born; each original live cell has two neighbors and survives.

Example 3

board = [[0,0,0],[1,1,1],[0,0,0]]return = [[0,1,0],[0,1,0],[0,1,0]]

The horizontal blinker becomes vertical when every cell reads the same original generation.

Constraints

  • 1 <= board.length, board[r].length <= 200.
  • Every row has equal length and every cell is 0 or 1.
  • The update must use O(1) auxiliary space beyond the returned board.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] gameOfLife(int[][] board) {
        // write your code here
    }
}
Checking account…