Game of Life: In-Place Next Generation
Updated
Problem statement
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]]Moved or new
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]]Moved or new
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]]Moved or new
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
0or1. - The update must use
O(1)auxiliary space beyond the returned board.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- 2048 Simulation and Board CodecCitadel · ONSITE INTERVIEWCitadel interview questions
- Diagonal Matrix IteratorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Candy Crush Board Until StableBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Game of Life Next GenerationTemporal · ONSITE INTERVIEWTemporal interview questions
- Minesweeper Board UpdateClay · ONSITE INTERVIEW