Simulate 8 Cells Under a Neighbor-Match Rule for Up to a Billion Days

Quick Overview

Simulate a row of eight prison cells where each interior cell becomes occupied when its two neighbors match, and return the state after n days with n up to one billion. It tests simultaneous state updates and handling a very large day count without stepping through every day.

Simulate 8 Cells Under a Neighbor-Match Rule for Up to a Billion Days

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of 8 prison cells, where `1` represents an occupied cell and `0` represents a vacant cell, simulate the state of the cells after `n` days. Each day, a cell becomes occupied (`1`) if both adjacent cells are the same (both `1` or both `0`), and becomes vacant (`0`) otherwise. The first and last cells (`cells[0]` and `cells[7]`) will always become `0` as they don't have two adjacent cells. Return the state of the cells after `n` days. ### Function Signature ```python def prison_after_n_days(cells: list[int], n: int) -> list[int]: ``` ### Rules - All 8 cells change at the same moment. Each day's state is computed only from the previous day's state, never from cells already updated that day. - The input is the state on day 0, and its end cells may be `0` or `1`. From day 1 on, `cells[0]` and `cells[7]` are always `0`. ### Constraints - `cells.length == 8` - `cells[i]` is either `0` or `1` - `1 <= n <= 10^9` ### Examples ```text Input: cells = [0, 1, 0, 1, 1, 0, 0, 1], n = 7 Output: [0, 0, 1, 1, 0, 0, 0, 0] ``` Applying the rule once gives `[0, 1, 1, 0, 0, 0, 0, 0]` on day 1, and continuing day by day reaches `[0, 0, 1, 0, 1, 1, 0, 0]` on day 6. One more day turns that into the output. ```text Input: cells = [1, 0, 0, 1, 0, 0, 1, 0], n = 1000000000 Output: [0, 0, 1, 1, 1, 1, 1, 0] ``` Applying the rule 1,000,000,000 times from the input ends on this state. It is also the state the rule produces on day 6. ```text Input: cells = [1, 1, 1, 1, 1, 1, 1, 1], n = 1 Output: [0, 1, 1, 1, 1, 1, 1, 0] ``` Every interior cell has two occupied neighbors, so it stays occupied. The two end cells have only one neighbor each, so they become vacant.

Overview: Simulate a row of eight prison cells where each interior cell becomes occupied when its two neighbors match, and return the state after n days with n up to one billion. It tests simultaneous state updates and handling a very large day count without stepping through every day.

Amazon logo
Amazon
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of 8 prison cells, where 1 represents an occupied cell and 0 represents a vacant cell, simulate the state of the cells after n days. Each day, a cell becomes occupied (1) if both adjacent cells are the same (both 1 or both 0), and becomes vacant (0) otherwise. The first and last cells (cells[0] and cells[7]) will always become 0 as they don't have two adjacent cells.

Return the state of the cells after n days.

Function Signature

def prison_after_n_days(cells: list[int], n: int) -> list[int]:

Rules

  • All 8 cells change at the same moment. Each day's state is computed only from the previous day's state, never from cells already updated that day.
  • The input is the state on day 0, and its end cells may be 0 or 1 . From day 1 on, cells[0] and cells[7] are always 0 .

Constraints

  • cells.length == 8
  • cells[i] is either 0 or 1
  • 1 <= n <= 10^9

Examples

Input: cells = [0, 1, 0, 1, 1, 0, 0, 1], n = 7
Output: [0, 0, 1, 1, 0, 0, 0, 0]

Applying the rule once gives [0, 1, 1, 0, 0, 0, 0, 0] on day 1, and continuing day by day reaches [0, 0, 1, 0, 1, 1, 0, 0] on day 6. One more day turns that into the output.

Input: cells = [1, 0, 0, 1, 0, 0, 1, 0], n = 1000000000
Output: [0, 0, 1, 1, 1, 1, 1, 0]

Applying the rule 1,000,000,000 times from the input ends on this state. It is also the state the rule produces on day 6.

Input: cells = [1, 1, 1, 1, 1, 1, 1, 1], n = 1
Output: [0, 1, 1, 1, 1, 1, 1, 0]

Every interior cell has two occupied neighbors, so it stays occupied. The two end cells have only one neighbor each, so they become vacant.