Settled coin tray topples
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.
token_tray_settle(tray: list[list[int]]) → list[list[int]][[[0,0,0],[0,4,0],[0,0,0]]]out[[0,1,0],[1,0,1],[0,1,0]][[[3,3],[3,3]]]out[[3,3],[3,3]][[[16]]]out[[0]]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,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