Design Tic-Tac-Toe With K Players
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design and implement a game engine, manage board state and turn logic, validate moves, and apply efficient data structures and algorithms for win detection within the Coding & Algorithms domain.
Constraints
- 1 <= n <= 10^9
- 1 <= k <= 10^5
- 0 <= len(moves) <= 2 * 10^5
- Each move is a list of three integers: [player, row, col]
- Row and column in the input may be outside the board and should then be treated as an invalid move
- Your approach should be efficient for large n, so avoid scanning the whole board after every move
Examples
Input: (3, 2, [])
Expected Output: []
Explanation: No moves are made, so there are no statuses to report.
Input: (5, 2, [[1, 0, 0], [2, 1, 0], [1, 0, 1], [2, 1, 1], [1, 0, 2], [2, 2, 2]])
Expected Output: ['Continue', 'Continue', 'Continue', 'Continue', '1 won', 'Game Over']
Explanation: Player 1 forms three consecutive marks horizontally at (0,0), (0,1), and (0,2). Any later move returns 'Game Over'.
Input: (4, 2, [[1, 0, 0], [2, 0, 0], [2, 0, 1], [1, 0, 2], [1, 1, 1], [2, 1, 0], [1, 2, 0]])
Expected Output: ['Continue', 'Invalid move', 'Continue', 'Continue', 'Invalid move', 'Continue', 'Continue']
Explanation: The second move is invalid because the cell is occupied. Since invalid moves do not advance the turn, the fifth move is also invalid because it is still player 2's turn.
Input: (5, 3, [[1, 0, 4], [2, 0, 0], [3, 4, 4], [1, 1, 3], [2, 1, 0], [3, 4, 3], [1, 2, 2]])
Expected Output: ['Continue', 'Continue', 'Continue', 'Continue', 'Continue', 'Continue', '1 won']
Explanation: Player 1 wins on the down-left diagonal with marks at (0,4), (1,3), and (2,2).
Input: (2, 3, [[1, 0, 0], [2, 2, 1], [2, 1, 1], [3, 0, 1]])
Expected Output: ['Continue', 'Invalid move', 'Continue', 'Continue']
Explanation: The second move is outside the 2x2 board. Since the board is smaller than 3x3, no player can ever form 3 consecutive marks.
Hints
- Only the latest valid move can create a new 3-in-a-row, so you never need to rescan the entire board.
- For large boards, store only occupied cells in a hash map keyed by (row, col), then check the 4 line directions around the new move.