Update a grid of firing neurons
I was asked this
No reports yet. Sign in to say where you were asked it.
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.
- a firing cell with exactly 3 firing neighbours becomes
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 <= 3000 <= 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 + oldwithKlarger than any value, read neighbours with% K, then a second pass divides byK. 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
tsteps. 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
What it tests
matrices · simulation · in-place
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
