Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Tic-Tac-Toe With N Board Size
00:00
5 left

Tic-Tac-Toe With N Board Size

HardPython

Problem

Wise. Energy uses a configurable grid game for testing energy-planning strategies. Given an n x n board, two players alternate placing marks, and a player wins by forming x contiguous marks horizontally, vertically, or diagonally.

Implement a function that processes the moves in order and returns the first winner. Return "A" or "B" when that player first forms a winning line, otherwise return "Draw" after all supplied moves are processed.

Formal Specification

Implement tic_tac_toe(n, x, moves), where n and x are integers and moves is a list of [row, column] pairs. Rows and columns are zero-indexed. Player A makes moves at even indices, and player B makes moves at odd indices. Every move is valid, targets an empty cell, and the game stops immediately after a winning move. The function returns a string.

A winning line may extend in either direction from the latest move, but all x cells must belong to the same player and share one of four orientations: horizontal, vertical, diagonal, or anti-diagonal.

Constraints

  • 1 <= x <= n <= 1000
  • 1 <= len(moves) <= n * n
  • Each coordinate satisfies 0 <= row, column < n
  • Moves contain no duplicates
  • Players alternate, with A moving first
  • The move list stops immediately after a win

Function Signature

def tic_tac_toe(n, x, moves):
Interviewer

Your question is Tic-Tac-Toe With N Board Size. 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.