Game of Life on a Finite and an Infinite Board
Updated
Problem statement
In the Game of Life, a board is a grid of cells. Each cell is either live (1) or dead (0). A cell's neighbors are the eight cells next to it: horizontally, vertically and diagonally. All cells change at the same moment to form the next generation:
- A live cell with fewer than two live neighbors dies.
- A live cell with two or three live neighbors stays live.
- A live cell with more than three live neighbors dies.
- A dead cell with exactly three live neighbors becomes live.
The problem comes in two parts, and one function answers both. The flag infinite tells you which part a call belongs to.
- Part 1 (
infinite = false):boardis anm x ngrid of0and1values. Cells outside the grid count as dead. Updateboardin place to the next generation, using only constant extra memory, and return it. - Part 2 (
infinite = true): the board has no edges and goes on forever in every direction, and almost every cell is dead. Nowboardlists the live cells, each as a pair[row, col]. Every cell not in the list is dead. Return the live cells of the next generation as[row, col]pairs, sorted by row and then by column. A new live cell may lie just outside the range of the input coordinates. If no cell is live, return an empty list.
Function
gameOfLife(board: int[][], infinite: boolean) → int[][]Examples
Example 1
board = [[0,1,0,0],[0,0,1,0],[1,1,1,0],[0,0,0,0]]infinite = falsereturn = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,1,0,0]]Part 1. Cells (0, 1) and (2, 0) each have one live neighbor, so they die. Cell (1, 2) has three live neighbors and stays live, and (2, 2) has two and stays live too. (2, 1) has three and stays live. The dead cells (1, 0) and (3, 1) each have three live neighbors, so they become live. The function changes board and returns it.
Example 2
board = [[-1,0],[0,0],[1,0]]infinite = truereturn = [[0,-1],[0,0],[0,1]]Part 2. The live cells form a short vertical line. The middle cell (0, 0) has two live neighbors and stays live. The two end cells have one each and die. The dead cells (0, -1) and (0, 1) each touch all three live cells, so they become live. The line turns sideways.
Example 3
board = [[1000000000,-1000000000],[5,5],[5,6],[6,5]]infinite = truereturn = [[5,5],[5,6],[6,5],[6,6]]Part 2. The first cell is far from the others and has no live neighbors, so it dies. Each of the other three cells has two live neighbors and stays live. The dead cell (6, 6) has three live neighbors, so it becomes live. The answer is sorted by row, then by column.
Constraints
- Part 1:
m == board.lengthandn == board[i].length - Part 1:
1 <= m, n <= 1000 - Part 1:
board[i][j]is0or1 - Part 2:
1 <= board.length <= 104 - Part 2:
board[i].length == 2 - Part 2:
-109 <= board[i][0], board[i][1] <= 109 - Part 2: all
board[i]are distinct
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- First Completed Mingo LineEpic · OAEpic interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Logger Rate Limiter with Every-Arrival HistoryGoogle · PHONE SCREENGoogle interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Realized Gains from a Real-Estate Transaction StreamKikoff · PHONE SCREENKikoff interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences