FastPrepGame of Life Next Generation

Game of Life Next Generation

Temporal logoTemporal● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a finite binary grid, compute one simultaneous generation of Conway's Game of Life. Every cell has up to eight neighbors.

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

Return the updated grid. All decisions must use the original generation.

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]]

Apply all births and deaths simultaneously.

Example 2

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

The dead corner has three live neighbors and is born.

Example 3

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

The isolated cell dies from underpopulation.

Constraints

  • 1 <= board.length, board[i].length <= 200.
  • Every cell is 0 or 1, and the grid is rectangular.

More Temporal problems

See Temporal hiring insights
public int[][] gameOfLife(int[][] board) {
    // write your code here
}
board[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
expected[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Checking account…