Find the winner of a 3x3 tic-tac-toe game where pieces drop down their column

Quick Overview

Simulate a 3x3 tic-tac-toe variant where every piece drops to the lowest empty cell of its column, then report whether Player A or Player B won, the game is a draw, or it is still pending. Tests column height tracking, win detection on rows, columns and diagonals, and handling moves into full columns.

Find the winner of a 3x3 tic-tac-toe game where pieces drop down their column

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Two players take turns playing Connect Tic-Tac-Toe, a gravity variant of tic-tac-toe, on a 3 x 3 grid. Player A always goes first and places `X`, and player B places `O`. Players alternate placing their symbols. Unlike standard tic-tac-toe, this variant has gravity: every symbol falls as far down its column as it can, settling on the bottom of the grid or on top of another symbol. A move chooses a column, and the symbol lands in the lowest empty cell of that column. Because every symbol lands as low as it can, no column ever has a gap. Given an array `moves` where each `moves[i] = [row_i, col_i]` describes the intended cell for the i-th move (player A on even indices, player B on odd indices), simulate the game with gravity and return: - `"A"` if player A wins (three `X` symbols in a row, column, or diagonal), - `"B"` if player B wins (three `O` symbols in a row, column, or diagonal), - `"Draw"` if all 9 cells are filled with no winner, - `"Pending"` if the game has not finished and no winner has been determined. Because of gravity, the `row_i` value in `moves[i]` is ignored for placement: the piece is dropped into column `col_i` and lands on the lowest available row of that column. Row 0 is the top row and row 2 is the bottom row. ### Function Signature ```python def gravity_tic_tac_toe_winner(moves: list[list[int]]) -> str: ``` ### Rules - Move `i` belongs to player A (`X`) when `i` is even and to player B (`O`) when `i` is odd, whatever happened on earlier moves. - If column `col_i` is already full, the move places nothing and is skipped. That player's turn is used up, and the next move still belongs to the player given by its own index. - The game ends as soon as a player has three of their symbols in a line. That player is the answer, and any later moves are ignored. - If nobody has won after all moves, return `"Draw"` when all 9 cells are filled and `"Pending"` otherwise. ### Constraints - `1 <= len(moves) <= 30` - `len(moves[i]) == 2` - `0 <= row_i, col_i <= 2` - All moves are within a 3 x 3 grid. ### Examples ```text Input: moves = [[0,0],[0,1],[0,0],[0,1],[0,0]] Output: "A" Final board: X . . X O . X O . ``` A drops three pieces into column 0 and B drops two into column 1. A's pieces stack up to fill column 0, so A wins on the fifth move. ```text Input: moves = [[0,0],[0,1],[0,2],[0,0],[0,1],[0,2]] Output: "Pending" Final board: . . . O X O X O X ``` A plays columns 0, 2 and 1, and B plays columns 1, 0 and 2. No line holds three matching symbols and three cells are still empty. ```text Input: moves = [[0,0],[0,0],[0,0],[0,0],[0,1],[0,1],[0,2]] Output: "A" Final board: X . . O O . X X X ``` Column 0 is full after the first three moves, so B's move at index 3 is skipped. A plays column 1 at index 4, B stacks on top of it at index 5, and A completes the bottom row at index 6.

Overview: Simulate a 3x3 tic-tac-toe variant where every piece drops to the lowest empty cell of its column, then report whether Player A or Player B won, the game is a draw, or it is still pending. Tests column height tracking, win detection on rows, columns and diagonals, and handling moves into full columns.

Community answers

Answer by hamdazam1

def gravity_tic_tac_toe_winner(moves: list[list[int]]) -> str: grid = [[None for in range(3)] for in range(3)] n = len(moves) n_a = 0 A = [0, 0, 0, 0, 0, 0, 0, 0] B = [0, 0, 0, 0, 0, 0, 0, 0] for i in range(n): _, c_d = moves[i][0], moves[i][1] move_skipped = True r_a = float('inf') for j in range(2, -1, -1): val = grid[j][c_d] if val == None: move_skipped = False r_a = j break if not move_skipped: n_a += 1 if i % 2 == 0: grid[r_a][c_d] = "X" A[r_a] += 1 A[c_d + 3] += 1 if (r_a, c_d) == (0, 0) or (r_a, c_d) == (1, 1) or (r_a, c_d) == (2, 2): A[6] += 1 if (r_a, c_d) == (2, 0) or (r_a, c_d) == (1, 1) or (r_a, c_d) == (0, 2): A[7] += 1 if A[r_a] == 3 or A[c_d + 3] == 3 or A[6] == 3 or A[7] == 3: return "A" else: grid[r_a][c_d] = "O" B[r_a] += 1 B[c_d + 3] += 1 if (r_a, c_d) == (0, 0) or (r_a, c_d) == (1, 1) or (r_a, c_d) == (2, 2): B[6] += 1 if (r_a, c_d) == (2, 0) or (r_a, c_d) == (1, 1) or (r_a, c_d) == (0, 2): B[7] += 1 if B[r_a] == 3 or B[c_d + 3] == 3 or B[6] == 3 or B[7] == 3: return "B" if n_a < 9: return "Pending" return "Draw"
Jane Street logo
Jane Street
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
6
0

Two players take turns playing Connect Tic-Tac-Toe, a gravity variant of tic-tac-toe, on a 3 x 3 grid. Player A always goes first and places X, and player B places O. Players alternate placing their symbols.

Unlike standard tic-tac-toe, this variant has gravity: every symbol falls as far down its column as it can, settling on the bottom of the grid or on top of another symbol. A move chooses a column, and the symbol lands in the lowest empty cell of that column. Because every symbol lands as low as it can, no column ever has a gap.

Given an array moves where each moves[i] = [row_i, col_i] describes the intended cell for the i-th move (player A on even indices, player B on odd indices), simulate the game with gravity and return:

  • "A" if player A wins (three X symbols in a row, column, or diagonal),
  • "B" if player B wins (three O symbols in a row, column, or diagonal),
  • "Draw" if all 9 cells are filled with no winner,
  • "Pending" if the game has not finished and no winner has been determined.

Because of gravity, the row_i value in moves[i] is ignored for placement: the piece is dropped into column col_i and lands on the lowest available row of that column. Row 0 is the top row and row 2 is the bottom row.

Function Signature

def gravity_tic_tac_toe_winner(moves: list[list[int]]) -> str:

Rules

  • Move i belongs to player A ( X ) when i is even and to player B ( O ) when i is odd, whatever happened on earlier moves.
  • If column col_i is already full, the move places nothing and is skipped. That player's turn is used up, and the next move still belongs to the player given by its own index.
  • The game ends as soon as a player has three of their symbols in a line. That player is the answer, and any later moves are ignored.
  • If nobody has won after all moves, return "Draw" when all 9 cells are filled and "Pending" otherwise.

Constraints

  • 1 <= len(moves) <= 30
  • len(moves[i]) == 2
  • 0 <= row_i, col_i <= 2
  • All moves are within a 3 x 3 grid.

Examples

Input: moves = [[0,0],[0,1],[0,0],[0,1],[0,0]]
Output: "A"

Final board:
X . .
X O .
X O .

A drops three pieces into column 0 and B drops two into column 1. A's pieces stack up to fill column 0, so A wins on the fifth move.

Input: moves = [[0,0],[0,1],[0,2],[0,0],[0,1],[0,2]]
Output: "Pending"

Final board:
. . .
O X O
X O X

A plays columns 0, 2 and 1, and B plays columns 1, 0 and 2. No line holds three matching symbols and three cells are still empty.

Input: moves = [[0,0],[0,0],[0,0],[0,0],[0,1],[0,1],[0,2]]
Output: "A"

Final board:
X . .
O O .
X X X

Column 0 is full after the first three moves, so B's move at index 3 is skipped. A plays column 1 at index 4, B stacks on top of it at index 5, and A completes the bottom row at index 6.