Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.