Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Tic Tac Toe Coding
00:00
5 left

Tic Tac Toe Coding

MediumPython

Problem

Chewy is prototyping a Tic Tac Toe feature for a customer-facing game experience. Given the board size and the moves played in order, determine whether player A wins, player B wins, the game ends in a draw, or play is still pending.

The board is an n x n grid. Players alternate turns, with A moving first. A player wins when all cells in any row, column, main diagonal, or anti-diagonal belong to that player. The input contains only valid moves, and no moves occur after the game has ended.

Formal Specification

Implement tic_tac_toe(n, moves), where n is an integer and moves is a list of coordinate pairs [row, column]. Return one of the strings "A", "B", "Draw", or "Pending".

Do not allocate an n x n board. Track only the information needed to determine whether the latest move completes a winning line.

Constraints

  • 3 <= n <= 1000
  • 0 <= len(moves) <= n * n
  • Each move is a valid pair [row, column]
  • 0 <= row, column < n
  • Moves alternate between players A and B
  • No move occurs after a winner is determined

Function Signature

def tic_tac_toe(n, moves):
Interviewer

Your question is Tic Tac Toe Coding. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.