Next Generation of a Cell Grid
Updated
Problem statement
You are given an m x n grid board. Each cell is alive (1) or dead (0). The neighbors of a cell are the up to 8 cells around it: up, down, left, right and the four diagonals. Cells outside the grid do not exist, so they never count as live neighbors.
Every cell changes at the same moment, based only on the current grid:
- A live cell stays alive if 2 or 3 of its neighbors are live.
- A live cell that has 0 or 1 live neighbors, or 4 or more, becomes dead.
- A dead cell comes alive if exactly 3 of its neighbors are live.
- Every other dead cell stays dead.
Return the next grid: a new m x n grid that shows every cell after this one change.
Function
gameOfLife(board: int[][]) → int[][]Examples
Example 1
board = [[0,0,0,0],[1,1,1,0],[0,0,0,0]]return = [[0,1,0,0],[0,1,0,0],[0,1,0,0]]The middle live cell (row 1, column 1) has 2 live neighbors, so it stays alive. The live cells at (1, 0) and (1, 2) have 1 live neighbor each, so they die. The dead cells at (0, 1) and (2, 1) each touch all 3 live cells, so they come alive. Every other cell has fewer than 3 live neighbors and stays dead.
Example 2
board = [[1,1,1],[1,1,1]]return = [[1,0,1],[1,0,1]]Each corner cell touches 3 live cells, so it stays alive. Each cell in the middle column touches 5 live cells, more than 3, so it dies.
Example 3
board = [[0,0,0],[0,1,0]]return = [[0,0,0],[0,0,0]]The only live cell, at (1, 1), has 0 live neighbors, so it dies. Every dead cell touches at most 1 live cell, so none comes alive. The answer is all 0.
Constraints
m == board.lengthandn == board[i].lengthfor every row1 <= m, n <= 50board[i][j]is0or1
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Diagonal TraverseHive · PHONE SCREENHive interview questions
- Build a Bird’s-Eye-View Height GridNVIDIA · PHONE SCREENNVIDIA interview questions
- Multi-Channel Valid ConvolutionTesla · PHONE SCREENTesla interview questions
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)