1275. Find Winner on a Tic Tac Toe Game - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

You are given a 2D integer array moves where each element is a pair [row, col] representing the position where a move is made on a 3x3 Tic Tac Toe board. The moves are played in order:

  • The 1st move is made by player A
  • The 2nd move is made by player B
  • The players alternate thereafter

Your task is to determine the outcome of the game after all the moves have been played. The outcome should be:

  • "A" if player A wins,
  • "B" if player B wins,
  • "Draw" if all 9 moves are played and neither player wins,
  • "Pending" if the game is not yet finished (i.e. fewer than 9 moves have been played and there is no winner).

A player wins if they have three of their marks in a row (vertically, horizontally, or diagonally).

Hints

  1. Winning Conditions:
    To determine if a player wins, you need to check:

    • Each of the 3 rows,
    • Each of the 3 columns,
    • The main diagonal (from top-left to bottom-right), and
    • The anti-diagonal (from top-right to bottom-left).
  2. Optimized Check with Counters:
    Instead of building and scanning a full 3x3 board, you can maintain counters for rows, columns, and both diagonals.

    • When player A makes a move, add +1 to the relevant counters.
    • When player B makes a move, subtract 1.
    • If any counter reaches +3, then A wins; if any counter reaches –3, then B wins.
  3. Game Outcome Determination:

    • If a winning condition is met during the moves, return the corresponding player immediately.
    • If no winner is found and all 9 moves are played, return "Draw".
    • If no winner is found and fewer than 9 moves have been played, return "Pending".

Approach: Counter-Based Method

Explanation

  1. Initialize Counters:

    • Rows: An array of 3 integers, each representing the net score (A's count minus B's count) for that row.
    • Columns: An array of 3 integers for the columns.
    • Diagonal: A single integer for the main diagonal.
    • Anti-diagonal: A single integer for the anti-diagonal.
  2. Process Each Move:

    • Iterate over the moves. For the (i)th move:
      • Determine the current player based on whether (i) is even (player A) or odd (player B).
      • For player A, add +1; for player B, add –1.
      • Update the counters for the row, column, and if applicable, the diagonal and anti-diagonal.
      • After each update, check if the absolute value of any counter reaches 3. If yes, declare the winner immediately (if the counter is +3, A wins; if –3, B wins).
  3. Determine the Outcome:

    • If all moves are processed without any counter reaching 3:
      • If 9 moves were played, the game is a "Draw".
      • Otherwise, the game is "Pending".

Why This Works

  • Efficiency:
    You only process each move once (O(1) per move) with constant-time updates and checks. Since there are at most 9 moves, the solution is very efficient.

  • Correctness:
    Using counters effectively aggregates the contribution of each move in a way that if one player controls an entire row, column, or diagonal, the corresponding counter will reach +3 (or –3 for player B).

Code Solutions

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:

    • Each move is processed in constant time (O(1)), and there are at most 9 moves. Therefore, the time complexity is (O(1)) in practice.
  • Space Complexity:

    • We use four counters (three arrays for rows and columns and two integers for diagonals) which take constant space (O(1)).

Step-by-Step Walkthrough

Consider the moves: [[0,0],[2,0],[1,1],[2,1],[2,2]].

  1. Move 0 (A at [0,0]):
    • rows[0] becomes +1, cols[0] becomes +1, diag becomes +1.
  2. Move 1 (B at [2,0]):
    • rows[2] becomes -1, cols[0] becomes +1 - 1 = 0, anti_diag becomes -1 (since 2+0 == 2, which is the anti-diagonal for a 3x3 board).
  3. Move 2 (A at [1,1]):
    • rows[1] becomes +1, cols[1] becomes +1, diag becomes +2 (since 1==1), anti_diag becomes 0 (1+1==2).
  4. Move 3 (B at [2,1]):
    • rows[2] becomes -1 - 1 = -2, cols[1] becomes +1 - 1 = 0.
  5. Move 4 (A at [2,2]):
    • rows[2] becomes -2 + 1 = -1, cols[2] becomes +1, diag becomes +3 (since 2==2).
    • The absolute value of diag becomes 3, so player A wins.

Common Pitfalls

  • Incorrect Player Determination:
    Ensure that moves are correctly alternated between players (A for even-indexed moves, B for odd-indexed moves).

  • Diagonal Checks:
    Remember that a move belongs to the main diagonal if row == col and to the anti-diagonal if row + col == 2 for a 3x3 board.

  • Edge Cases:

    • Fewer than 5 moves: It is impossible for any player to win.
    • Exactly 9 moves without a win: The game is a "Draw".

Relevant Problems

  • Design Tic Tac Toe: A problem where you design a Tic Tac Toe game class that supports player moves and dynamically checks for a winner.

  • Valid Tic Tac Toe State: Given a Tic Tac Toe board state, determine if it is valid based on the game rules.

  • Tic Tac Toe Checker: Problems that involve checking if a board has a winning configuration, which is similar to verifying win conditions.

  • Board Game Simulations: Other problems involving grid-based game state evaluations and simulation logic.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How do I describe my industry?
What does the 'static' keyword do in a class?
Emphasizing maintainability in architectural decision-making
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;