Detect a Winner on a 3x3 Tic-Tac-Toe Board
Company: Hebbia
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
# Detect a Winner on a 3x3 Tic-Tac-Toe Board
Implement `tic_tac_toe_winner(board)` for a completed or partially completed 3x3 board. Each cell is `"X"`, `"O"`, or `"."`. Return `"X"` if X has a complete row, column, or diagonal, return `"O"` if O does, and otherwise return `"None"`.
You may assume the input board is reachable from some alternating sequence of legal moves and therefore cannot contain simultaneous winners.
## Input
- `board`: a list of exactly three strings, each of length three.
## Output
- One of `"X"`, `"O"`, or `"None"`.
## Constraints
- `board.length == 3`
- `board[i].length == 3`
- Every character is `X`, `O`, or `.`.
```hint Enumerate complete lines
The winning conditions are the three rows, three columns, and two diagonals. Check that all three cells in a line equal the same non-empty mark.
```
Quick Answer: A coding interview problem about detecting the winner on a 3x3 Tic-Tac-Toe board. It tests careful handling of rows, columns, diagonals, empty cells, and board states where neither player has won.
Implement `tic_tac_toe_winner(board)` for a completed or partially completed 3x3 Tic-Tac-Toe board. Each cell is `"X"`, `"O"`, or `"."`. Return `"X"` if X has a complete row, column, or diagonal; return `"O"` if O does; otherwise return `"None"`.
The board is guaranteed to be reachable through alternating legal moves, so it cannot contain simultaneous winners. The return value is therefore unique for every valid input.
Constraints
- board contains exactly 3 strings.
- Each string contains exactly 3 characters.
- Every character is X, O, or . (an empty cell).
- The board is reachable from an alternating sequence of legal moves and has at most one winner.
- The function must not modify board.
Examples
Input: (['...', '...', '...'],)
Expected Output: 'None'
Explanation: An empty board contains no complete non-empty line.
Input: (['...', '.X.', '...'],)
Expected Output: 'None'
Explanation: A singleton center move is not a winning line.
Hints
- List every kind of straight line that can win before writing the checks.
- A line of three equal cells wins only when that shared cell is not the empty marker.
- The fixed 3x3 board has eight candidate winning lines in total.