**Tic Tac Toe - Amazon Top Interview Questions**

### Problem Statement :

Implement the tic-tac-toe game with the following methods: TicTacToe(int n) which instantiates an n x n game board. A player wins a game if their pieces either form a horizontal, vertical, or diagonal line of length n. int move(int r, int c, boolean me) which places the next move at row r and column c. me indicates whether it's my move (me = true) or it's your opponent's (me = false) move. If this move makes you win, return 1, if your opponent wins, return -1, and otherwise return 0. Constraints 1 ≤ n ≤ 100,000 0 ≤ m ≤ 100,000 where m is the number of calls to move Example 1 Input methods = ["constructor", "move", "move", "move", "move", "move"] arguments = [[3], [0, 0, True], [2, 0, False], [0, 1, True], [2, 1, False], [0, 2, True]]` Output [None, 0, 0, 0, 0, 1] Explanation t = TicTacToe(3) t.move(0, 0, True) == 0 # I place piece (0, 0) t.move(2, 0, False) == 0 # Opponent places piece (2, 0) t.move(0, 1, True) == 0 # I place piece (0, 1) t.move(2, 1, False) == 0 # Opponent places piece (2, 1) t.move(0, 2, True) == 1 # I place piece (0, 2) to win

### Solution :

` ````
Solution in C++ :
class TicTacToe {
int n;
unordered_map<int, int> row[2], col[2], diag[2], cross[2];
public:
TicTacToe(int n) : n(n) {
}
int move(int r, int c, bool me) {
int winner = 2 * me - 1;
if (++row[me][r] == n) return winner;
if (++col[me][c] == n) return winner;
if (++diag[me][r + c] == n) return winner;
if (++cross[me][r - c] == n) return winner;
return 0;
}
};
```

` ````
Solution in Java :
import java.util.*;
class TicTacToe {
int n;
int[] rowSum;
int[] colSum;
int diag;
int revDiag;
public TicTacToe(int n) {
this.n = n;
rowSum = new int[n];
colSum = new int[n];
diag = 0;
revDiag = 0;
}
public int move(int r, int c, boolean me) {
// Step 1) Make a move
int val = me ? 1 : -1;
rowSum[r] += val;
colSum[c] += val;
if (r == c)
diag += val;
if (r == n - c - 1)
revDiag += val;
// Step 2) Check if game over
boolean gameOver = Math.abs(rowSum[r]) == n || Math.abs(colSum[c]) == n
|| Math.abs(diag) == n || Math.abs(revDiag) == n;
if (gameOver)
return me ? 1 : -1;
return 0;
}
}
```

` ````
Solution in Python :
class TicTacToe:
def __init__(self, n):
self.n = n
self.row_sums = [0] * n
self.col_sums = [0] * n
self.diag = self.inv_diag = 0
def move(self, r, c, me):
val = 1 if me else -1
# update values
self.row_sums[r] += val
self.col_sums[c] += val
if r == c:
self.diag += val
if r == self.n - c - 1:
self.inv_diag += val
# check wins
if self.n in list(map(abs, [self.row_sums[r], self.col_sums[c], self.diag, self.inv_diag])):
return val
return 0
```

## View More Similar Problems

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →