Simulate a Turn-Based Two-Player Game
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates API design, state management, move validation, board representation, and correctness guarantees for a turn-based game simulator. It falls under the Coding & Algorithms domain and is commonly asked to assess edge-case handling, time/space complexity awareness and practical application of algorithmic thinking and software design at the implementation level.
Constraints
- 1 <= n <= 100000
- 0 <= len(moves) <= 200000
- Each move is a tuple (player, row, col) where player is usually 'A' or 'B'; invalid values should be treated as invalid moves
- row and col may be outside the board and should then be treated as an invalid move
Examples
Input: (3, [('A', 0, 0), ('B', 1, 0), ('A', 0, 1), ('B', 1, 1), ('A', 0, 2)])
Expected Output: (['game continues', 'game continues', 'game continues', 'game continues', 'A wins'], 'A wins')
Explanation: A fills the entire top row on the fifth move.
Input: (2, [('A', 0, 0), ('A', 1, 1), ('B', 1, 1), ('A', 0, 1)])
Expected Output: (['game continues', 'game continues', 'game continues', 'A wins'], 'A wins')
Explanation: The second move is invalid because it is A's turn only once. The invalid move is ignored, B then moves, and A wins on the last move.
Input: (3, [('A', 0, 0), ('B', 0, 1), ('A', 0, 2), ('B', 1, 1), ('A', 1, 0), ('B', 1, 2), ('A', 2, 1), ('B', 2, 0), ('A', 2, 2)])
Expected Output: (['game continues', 'game continues', 'game continues', 'game continues', 'game continues', 'game continues', 'game continues', 'game continues', 'draw'], 'draw')
Explanation: All 9 cells are filled without any player completing a row, column, or diagonal.
Input: (3, [('A', 0, 0), ('B', 1, 0), ('A', 0, 1), ('B', 1, 1), ('A', 0, 2), ('B', 2, 2), ('A', 2, 1)])
Expected Output: (['game continues', 'game continues', 'game continues', 'game continues', 'A wins', 'A wins', 'A wins'], 'A wins')
Explanation: A wins on move 5. All later moves are invalid because the game has already ended, so the reported state stays 'A wins'.
Input: (3, [])
Expected Output: ([], 'game continues')
Explanation: No moves were made, so the game has not ended.
Input: (1, [('A', 0, 0), ('B', 0, 0)])
Expected Output: (['A wins', 'A wins'], 'A wins')
Explanation: On a 1 x 1 board, A wins immediately with the first valid move. Any later move is ignored.
Input: (3, [('A', 0, 0), ('B', 0, 0), ('B', 3, 1), ('B', 1, 1)])
Expected Output: (['game continues', 'game continues', 'game continues', 'game continues'], 'game continues')
Explanation: The second move targets an occupied cell, the third is outside the board, and only the fourth attempted move is a valid move by B.
Hints
- You do not need to scan the whole board after every move. Track counts for rows, columns, and the two diagonals.
- Be careful with invalid moves: they should not change the board, should not advance the turn, and should not undo a finished game.