← Questions

Update a grid of firing neurons

0
Scale AICodingLast asked Feb 2026

A coding interview question asked in Scale AI's coding round, last asked Feb 2026. It tests matrices, simulation and in-place.

You are given an m x n grid of non-negative integers describing neurons. A cell with value 0 is a firing neuron; a cell with a value above 0 is not firing. Compute the next state of the grid, applying every update at the same time from the current state. First solve it with a copy of the grid, then update it in place with O(1) extra space.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • update_neurons(neurons: list[list[int]]) -> list[list[int]] updates the grid and returns it.
  • For each cell, count the firing cells among its 8 neighbours (horizontal, vertical, diagonal). Then:
    • a firing cell with exactly 3 firing neighbours becomes 6;
    • a non-firing cell with 0 or 1 firing neighbours decreases by 2;
    • a non-firing cell with more than 3 firing neighbours decreases by 1;
    • no value goes below 0; every other cell keeps its value.

Example

update_neurons([[0, 1, 0],
                [2, 0, 9],
                [0, 1, 5]])   ->  [[0, 1, 0],
                                   [2, 6, 9],
                                   [0, 1, 3]]
# centre: firing, with exactly 3 firing neighbours -> 6
# bottom-right 5: one firing neighbour -> 5 - 2 = 3
# top-middle 1: 3 firing neighbours -> unchanged

update_neurons([[0, 0, 0], [0, 0, 0], [0, 0, 0]])  ->  [[6, 0, 6], [0, 0, 0], [6, 0, 6]]
update_neurons([[5]])  ->  [[3]]

Constraints

  • 1 <= m, n <= 300
  • 0 <= neurons[i][j]

Clarifying questions

  • Are updates simultaneous, based only on the state before this step?
  • Do edge cells simply have fewer neighbours (no wrap-around)?
  • Is "more than 3" strict? What happens to a non-firing cell with 2 or 3 firing neighbours?
  • Can values be large, or bounded?

What a strong answer covers

  • The copy version: read from a snapshot, write into the grid; O(mn) time and O(mn) extra space.
  • The in-place version: encode old and new state in each cell, for example new * K + old with K larger than any value, read neighbours with % K, then a second pass divides by K. Or mark cells with negative values when the range allows.
  • Exactly one rule per cell, with the floor at 0.
  • Edge cases: a 1×1 grid, one row, all firing; tests comparing the in-place version with the copy version on random grids.

Follow-ups

  • Run t steps. Can you stop early when the grid stops changing?
  • The grid is huge and sparse (few firing cells). How do you update only what can change?
  • The grid is infinite, given as a set of firing coordinates.

Where it's asked

  • Scale AI · Coding · Last asked Feb 2026Live coding, one or two rounds, Python

What it tests

matrices · simulation · in-place

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding