Quick Overview

This question evaluates a candidate's competence in designing efficient stateful data structures and algorithmic optimization for an n×n game implementation, focusing on time-space trade-offs and robust API design.

Implement a Tic-Tac-Toe game class

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Implement a class that supports playing an **n × n** Tic-Tac-Toe game. ### Requirements Create a class `TicTacToe(n)` with: - `move(row, col, player) -> int` - `row` and `col` are 0-indexed coordinates. - `player` is either `1` or `2`. - Each call places the player's mark at `(row, col)` (assume the move is always valid and the cell is empty). - Return: - `0` if there is no winner after this move, - `1` if player 1 wins after this move, - `2` if player 2 wins after this move. A player wins if they fill an entire **row**, **column**, **main diagonal**, or **anti-diagonal**. ### Constraints (typical interview assumptions) - `2 <= n <= 10^4` (so you should avoid O(n) scanning per move) - Many moves may be made; aim for **O(1)** time per `move` and **O(n)** space.

Overview: This question evaluates a candidate's competence in designing efficient stateful data structures and algorithmic optimization for an n×n game implementation, focusing on time-space trade-offs and robust API design.

Simulate n x n Tic-Tac-Toe moves and return winner status after each move.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: (3, [(0,0,1),(0,2,2),(1,1,1),(0,1,2),(2,2,1)])

Expected Output: [0, 0, 0, 0, 1]

Explanation: Player 1 wins diagonal.

Input: (2, [(0,1,2),(1,1,2)])

Expected Output: [0, 2]

Explanation: Player 2 wins column.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.

Tic-Tac-Toe Move Simulation

mediumMicrosoftSoftware Engineer

Simulate n x n Tic-Tac-Toe moves and return winner status after each move.

View full question (all parts)

Problem

Implement a class that supports playing an n × n Tic-Tac-Toe game.

Requirements

Create a class TicTacToe(n) with:

  • move(row, col, player) -> int
    • row and col are 0-indexed coordinates.
    • player is either 1 or 2.
    • Each call places the player's mark at (row, col) (assume the move is always valid and the cell is empty).
    • Return:
      • 0 if there is no winner after this move,
      • 1 if player 1 wins after this move,
      • 2 if player 2 wins after this move.

A player wins if they fill an entire row, column, main diagonal, or anti-diagonal.

Constraints (typical interview assumptions)

  • 2 <= n <= 10^4 (so you should avoid O(n) scanning per move)
  • Many moves may be made; aim for O(1) time per move and O(n) space.

Examples

Example 1
Input
(3, [(0,0,1),(0,2,2),(1,1,1),(0,1,2),(2,2,1)])
Output
[0, 0, 0, 0, 1]
Notes
Player 1 wins diagonal.
Example 2
Input
(2, [(0,1,2),(1,1,2)])
Output
[0, 2]
Notes
Player 2 wins column.

Constraints

  • •Inputs are Python literals matching the function signature.
  • •Return a deterministic exact-match value.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Sample call
solution(3, [(0,0,1),(0,2,2),(1,1,1),(0,1,2),(2,2,1)])
Language
Case 1
Input
(3, [(0,0,1),(0,2,2),(1,1,1),(0,1,2),(2,2,1)])
Expected
[0, 0, 0, 0, 1]
Case 2
Input
(2, [(0,1,2),(1,1,2)])
Expected
[0, 2]

Browse More Questions