Magicsheet logo

Find Winner on a Tic Tac Toe Game

Easy
56.6%
Updated 6/1/2025

Find Winner on a Tic Tac Toe Game

1. What is this problem about?

The Find Winner on a Tic Tac Toe Game interview question asks you to determine the state of a 3imes33 imes 3 game board after a sequence of moves. The two players, A and B, take turns placing their marks ('X' and 'O'). You need to return "A" or "B" if a player wins, "Draw" if the board is full with no winner, or "Pending" if moves remain and no one has won yet.

2. Why is this asked in interviews?

Companies like Apple and Microsoft use the Find Winner coding problem as a warm-up. It tests basic array indexing, simulation skills, and clean conditional logic. It evaluations whether you can efficiently check for win conditions (rows, columns, and diagonals) without redundant code. It’s a standard Simulation interview pattern.

3. Algorithmic pattern used

This problem follows the Matrix Win Condition pattern.

  • Counters: Instead of checking the whole board every time, you can maintain 8 counters: 3 for rows, 3 for columns, and 2 for diagonals.
  • Increment/Decrement: For Player A, increment the counter. For Player B, decrement it.
  • Win Check: If any counter reaches 3, A wins. If it reaches -3, B wins.
  • State Check: If no winner is found, check if the total moves equal 9 (Draw) or less (Pending).

4. Example explanation

Moves: [[0,0], [1,1], [0,1], [0,2], [1,0], [2,0]]

  • Player A marks: (0,0), (0,1), (1,0)
  • Player B marks: (1,1), (0,2), (2,0) Check rows for A:
  • Row 0 has (0,0) and (0,1). Count = 2.
  • Check columns for B:
  • Col 0 has (2,0). ... The simulation continues until a win is detected or moves run out.

5. Common mistakes candidates make

  • Inefficient checking: Writing 8 separate if statements instead of using a loop or counters.
  • Diagonal error: Forgetting that diagonals only exist for specific cells (e.g., (0,0),(1,1),(2,2)(0,0), (1,1), (2,2) and (0,2),(1,1),(2,0)(0,2), (1,1), (2,0)).
  • Draw vs Pending: Miscounting the number of moves and returning "Draw" when moves are still possible.

6. Interview preparation tip

For grid-based games, always separate the State Update logic from the Win Condition logic. This makes your code more modular and easier to adapt if the board size increases (e.g., a 100imes100100 imes 100 board).

Similar Questions