Code RoomSettled coin tray topples
MediumPrep Room Coding #4901

Settled coin tray topples

CodingAlgorithms & data structuresMid–Senior~22 min

A cash office settles coin trays on a sorting table laid out as a rectangular grid of pockets, where tray[r][c] holds the token count in that pocket. The sorter repeatedly picks any pocket holding 4 or more tokens and topples it: that pocket loses exactly 4 tokens, and one token is pushed into each of its four orthogonally adjacent pockets. A token pushed over the outer edge of the table falls into the reject bin and is gone for good. The sorter stops once no pocket holds 4 or more. Return the settled table. Every row has the same length and holds at least one pocket, and no count starts negative. The table always settles, and the settled table never depends on the order in which unstable pockets are picked, so any order you like gives the same answer.

Implement
token_tray_settle(tray: list[list[int]]) → list[list[int]]
Examples
in[[[0,0,0],[0,4,0],[0,0,0]]]out[[0,1,0],[1,0,1],[0,1,0]]
in[[[3,3],[3,3]]]out[[3,3],[3,3]]
in[[[16]]]out[[0]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 22 min
InputExpectedGot
[[[0,0,0],[0,4,0],[0,0,0]]][[0,1,0],[1,0,1],[0,1,0]]not run yetsample
[[[3,3],[3,3]]][[3,3],[3,3]]not run yetsample
[[[16]]][[0]]not run yetsample