Three-in-a-Row: Find the Winner on a 3×3 Grid
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests whether you can encode all win conditions for a 3×3 grid as data and apply a uniform check to identify a winning player.
Three-in-a-Row: Find the Winner on a 3×3 Grid
You are given a snapshot of a 3×3 game grid stored as a flat list of 9 integers. Position i in the list maps to row i // 3 and column i % 3. Each cell holds one of three values:
0— the cell is empty1— occupied by Player 12— occupied by Player 2
A player wins if they own every cell in any one row, any one column, or either of the two main diagonals.
Write a function that examines the board and returns:
1if Player 1 has a complete line2if Player 2 has a complete line0if neither player has won (whether the game is still in progress or has ended in a draw)
If the board is in an illegal state where both players appear to have completed a line, return the winner of the first matching line found when checking in this order: rows top-to-bottom, then columns left-to-right, then the main diagonal, then the anti-diagonal.
Constraints
boardhas exactly 9 elements.- Every element is
0,1, or2. - No other validation of board legality is required.
Examples
Example 1
Input: find_grid_winner([1, 1, 1, 0, 2, 0, 2, 0, 0])
Output: 1
Example 2
Input: find_grid_winner([1, 2, 0, 1, 2, 0, 0, 0, 0])
Output: 0
Hints
Hint 1
There are exactly 8 ways to win on a 3×3 grid. Write them all down as index triplets into the flat list before you write any logic.
Hint 2
For each triplet, a player owns that line if all three values are equal and at least one of them is non-zero. A line of three zeros must not count as a win.
Hint 3
Once you have the eight triplets, the solution body is a single loop: return the cell value on the first matching triplet, or return 0 after the loop exhausts all eight.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core idea: turn geometry into data
A 3×3 grid has exactly 8 lines a player can complete — 3 rows, 3 columns, and 2 diagonals. Instead of writing separate loops or special cases for each type, encode every line upfront as a triplet of flat-array indices:
| Line type | Triplets |
|---|---|
| Rows | (0,1,2) (3,4,5) (6,7,8) |
| Columns | (0,3,6) (1,4,7) (2,5,8) |
| Diagonals | (0,4,8) (2,4,6) |
With the geometry expressed as data, the logic shrinks to a single uniform pass: for each triplet (a, b, c), check whether all three board values are equal and non-zero. If so, that player owns the line — return their marker. If the loop ends with no match, return 0.
Why it is correct
Completeness. Every possible win on a 3×3 grid corresponds to one of these 8 triplets, so no winning configuration can slip through.
No false positives. The board[a] != 0 guard is essential. A run of three empty cells (0, 0, 0) satisfies a == b == c without the guard, which would trigger a spurious match. The guard ensures we only return when a real player owns all three cells.
Draw and in-progress both map to 0. The function returns early on the first winning line it finds. If no line qualifies, it exits the loop and returns 0. Both a full board with no winner and a board still being played produce 0, which is exactly what the contract requires.
Illegal double-winner boards. If both players somehow have a completed line (impossible in legal play), the function returns whoever owns the first matching line in the fixed scan order — rows, then columns, then diagonals. This is a defined, deterministic result without needing extra validation.
Complexity
- Time: O(1). There are always exactly 8 lines with 3 index lookups each — 24 reads regardless of board contents.
- Space: O(1). The line table is a fixed constant; no auxiliary data structure grows with input.
Tracing the sample cases
[1,1,1, 0,2,0, 2,0,0] — the first triplet (0,1,2) reads 1,1,1. Non-zero and all equal, so the function returns 1.
[1,2,0, 1,2,0, 0,0,0] — row triplets read (1,2,0), (1,2,0), (0,0,0), none matching. Column (0,3,6) reads 1,1,0 and (1,4,7) reads 2,2,0, neither complete. All remaining lines also fail. The function returns 0.
Reference solution
from typing import List
def find_grid_winner(board: List[int]) -> int:
# All 8 winning lines expressed as index triplets into the flat board.
lines = (
(0, 1, 2), (3, 4, 5), (6, 7, 8), # rows, top to bottom
(0, 3, 6), (1, 4, 7), (2, 5, 8), # columns, left to right
(0, 4, 8), (2, 4, 6), # main diagonal, anti-diagonal
)
for a, b, c in lines:
# All three cells must be equal AND non-zero (a line of empties is not a win).
if board[a] != 0 and board[a] == board[b] == board[c]:
return board[a]
# No complete line found: draw or game still ongoing.
return 0