Medium · System Design
Design Tic-Tac-Toe
Given n and a sequence of valid moves (row, col, player) on an n × n Tic-Tac-Toe board, where player is 1 or 2 and each move takes an empty cell, return for each move the player's number if that move completes a full row, column or diagonal of that player's marks, and 0 otherwise.
Examples
Example 1
{
"n": 3,
"moves": [
{"row": 0, "col": 0, "player": 1},
{"row": 1, "col": 0, "player": 2},
{"row": 0, "col": 1, "player": 1},
{"row": 1, "col": 1, "player": 2},
{"row": 0, "col": 2, "player": 1}
]
}Output: [0, 0, 0, 0, 1]
Example 2
{
"n": 3,
"moves": [
{"row": 0, "col": 0, "player": 1},
{"row": 1, "col": 1, "player": 2},
{"row": 0, "col": 1, "player": 1},
{"row": 0, "col": 2, "player": 2},
{"row": 1, "col": 2, "player": 1},
{"row": 2, "col": 0, "player": 2}
]
}Output: [0, 0, 0, 0, 0, 2]
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.