Chime | Phone | Senior SWE
Topple
Two players, player1 and player2, share an n x n board. Pieces are stacked, and a
tall enough stack can be knocked over — spilling across the board and capturing everything
it lands on. Given the size of the board and the full list of moves that were played, report
who has won, or that the game is still going.
The board
n x ncells,3 <= n <= 9.- Coordinates are
(x, y), both 0-indexed, with the origin at the top-left:x= column, increasing to the righty= row, increasing downward
- Because
n <= 9,xandyare always single digits.
Each cell is either empty or holds a stack: some number of pieces (the stack's
height) belonging to exactly one player. Pieces are indistinguishable — a stack has one
owner, and a player controls a cell if they own the stack sitting on it.
Notation used in this document: . is an empty cell, A3 is a player1 stack of height 3,
B1 is a player2 stack of height 1.
A turn
player1 moves first, and the players alternate turns.
A turn is one place, optionally followed by one topple. Toppling is never mandatory,
never happens more than once per turn, and only ever happens immediately after that turn's
place — you cannot topple on its own.
p{x}{y} — place
The player to move adds one piece to cell (x, y).
- If the cell is empty, it becomes a stack of height 1 owned by that player.
- If the cell is already owned by that player, its height increases by 1.
- A player may not place onto a cell the opponent controls. Toppling is the only way to
take enemy ground.
A p move always begins a new turn, so it is also what hands the turn to the other player.
t{x}{y}{dir} — topple
Knock the stack at (x, y) over in direction dir, one of U, D, L, R
(up = -y, down = +y, left = -x, right = +x).
Legal only if (x, y) is controlled by the player to move and its height is >= 2.
Resolve it like this — let k be the height of the toppled stack:
- Cell
(x, y)is emptied. Allkpieces are lifted off it. - Those
kpieces are laid down one at a time alongdir, one piece per cell, onto the
cells at distance1, 2, ..., kfrom(x, y). - Every cell a piece lands on is captured: its height goes up by 1 and its owner
becomes the toppling player. Landing a single piece on an opposing stack of height 5
yields a stack of height 6 belonging to you. - Any piece that would land off the board falls off and is removed from the game
permanently. A stack toppled toward a nearby edge destroys itself.
So a stack's height is both its value and its reach: k pieces travel exactly k cells.
Ending the game
After every move, check both players:
If a player controls zero cells and has already taken at least one turn, that player
loses, and the other player wins.
The "at least one turn" clause only exists to cover the opening — right after player1's very
first place, player2 controls nothing but has not played yet, and that is not a win.
Note this rule is symmetric, and cuts both ways: a player who topples their last stack off
the edge of the board eliminates themselves and loses. (Both players cannot reach zero on
the same move — the only way to empty an opponent is to capture their cells, which leaves you
holding pieces.)
If the move list is exhausted without either player being eliminated, the game is ongoing.
Input
n | integer, 3 <= n <= 9 — the board is n x n |
moves | list of move strings, in the order played |
Each move is either p{x}{y} (3 characters) or t{x}{y}{dir} (4 characters).
Output
Exactly one string:
"player1 is the winner""player2 is the winner""ongoing"
solve(n: int, moves: list[str]) -> strExamples
Example 1 — a capture wins it
n = 3
moves = ["p10", "p11", "p10", "t10D"]p10 (player1) p11 (player2) p10 (player1)
. A1 . . A1 . . A2 .
. . . . B1 . . B1 .
. . . . . . . . .player1 topples the height-2 stack at (1,0) downward. Two pieces travel two cells: the first
lands on (1,1), capturing player2's stack and making it A2; the second lands on the empty
(1,2), making it A1. (1,0) is left empty.
t10D (player1)
. . .
. A2 .
. A1 .player2 controls nothing and has taken a turn.
Output: "player1 is the winner"
Example 2 — pieces fall off the edge
n = 3
moves = ["p10", "p02", "p10", "t10R", "p02", "t02U"]player1 builds A2 at (1,0) and topples it right. The first piece lands on (2,0); the
second would land on (3,0), which is off the board, so it is destroyed. player1 is left with
a single piece.
after t10R (player1)
. . A1
. . .
B1 . .player2 answers by building B2 at (0,2) and toppling it up — both pieces stay on the
board, landing on (0,1) and (0,0).
after t02U (player2)
B1 . A1
B1 . .
. . .Both players still control cells, and the move list is finished.
Output: "ongoing"
Example 3 — toppling into the void
n = 3
moves = ["p00", "p22", "p00", "t00U"]after p00, p22, p00
A2 . .
. . .
. . B1player1 topples the corner stack upward. Both pieces are aimed off the top edge, so both
are destroyed and (0,0) is emptied. player1 now controls zero cells — and has taken a turn.
after t00U
. . .
. . .
. . B1Output: "player2 is the winner"
Example 4 — same idea, bigger board
n = 4
moves = ["p11", "p21", "p11", "t11R"]Output: "player1 is the winner"
Constraints and guarantees
3 <= n <= 90 <= len(moves) <= 10^5- Every move in the list is legal. You do not need to validate coordinates, ownership,
stack height, or turn order — no move places onto an opponent's cell, nottopples a
stack of height< 2or a stack the mover does not own, and notappears without ap
immediately before it in the same turn. - The move list never continues past a decided game: once a player is eliminated, that is the
last move in the list. - Stack heights are unbounded — a stack can grow taller than
nand spill most of its pieces
off the board. - An empty
moveslist is valid and yields"ongoing".

