Game of Life

Medium · Arrays & Strings · about 20 minutes

Asked at Meta, Amazon, Google, Microsoft, Adobe, Dropbox, Electronic Arts.

Given an m × n board of cells, 1 (live) or 0 (dead), compute the next generation of Conway's Game of Life. Each cell looks at its 8 neighbors:

  • A live cell with fewer than 2 live neighbors dies.
  • A live cell with 2 or 3 live neighbors lives on.
  • A live cell with more than 3 live neighbors dies.
  • A dead cell with exactly 3 live neighbors becomes live.

All cells update at the same moment. Update the board in place and return it.