← 返回 scale.ai 的题目列表Neuron Firing Cellular Automata
类型:qbank
Update a matrix of neurons using rules similar to Game of Life, but with Scale-specific firing and decrement rules. The first part permits a deep copy; the follow-up asks for improved space complexity.
Requirements
Input: a matrix of neurons.
A cell with value 0 is a firing neuron.
A cell with non-zero value is a non-firing neuron.
For each cell, inspect all neighboring spots and count how many neighbors are firing.
Update rules:
Any firing neuron becomes 6 if exactly three neighbors are firing.
Any non-firing neuron is decremented by 2 if one or zero neighbors are firing.
Any non-firing neuron is decremented by 1 if more than three neighbors are firing.
A neuron value cannot drop below zero.
Part 1 can deep-copy the whole matrix and write the next state into the copy.
Follow-up: optimize space complexity instead of deep-copying the entire matrix.
Notes
This is a LeetCode 289-style cellular-automata prompt with different state-transition rules.
Keep old-state reads and new-state writes separate. If optimizing in place, encode the old and new values together or store a compact delta so neighbor counts still use the original state.
Clarify the neighborhood definition before coding: candidates describe traversing all neighbors around each spot, so implement the standard 8-neighbor grid unless the interviewer narrows it.
Boundary handling is the common bug. Centralize neighbor iteration in a helper rather than copying bounds checks in multiple branches.
Preparation
Implement the deep-copy version first, then refactor to an in-place encoded-state version.
Drill LC 289 once, but replace the transition table with the Scale neuron rules above.
Prepare tests for corners, edges, all-zero grids, all-nonzero grids, value floor at zero, and exactly-three firing neighbors.