Find Winner on a Tic Tac Toe Game

EasyMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Two players, A and B, play tic-tac-toe on a 3x3 grid. A always moves first and marks X; B marks O. Players alternate, so moves[0] is A's, moves[1] is B's, and so on. Each moves[i] = [row, col] names an empty cell. The game stops as soon as someone fills a whole row, column, or diagonal with their mark, or when all nine cells are filled. The given moves always describe a legal game, and no moves appear after the game has ended. Return "A" or "B" if that player won, "Draw" if the grid is full with no winner, and "Pending" if there is no winner and empty cells remain.

Examples

Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: "A"
A holds (0,0), (1,1), (2,2): the main diagonal.
Input: moves = [[1,1],[0,0],[0,2],[2,0],[1,0],[1,2],[2,1],[0,1],[2,2]]
Output: "Draw"
The final grid is O O X / X X O / O X X. Every cell is filled and no line belongs to one player.
Input: moves = [[0,0],[1,1]]
Output: "Pending"
Nobody has a line yet and seven cells are empty.

Constraints

  • 1 <= moves.length <= 9
  • moves[i].length == 2
  • 0 <= row, col <= 2
  • All moves are distinct and form a legal game that has not continued past its end.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems