title-img


Tic Tac Toe - Amazon Top Interview Questions

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. Constr

View Solution →

Twin Trees - Amazon Top Interview Questions

Given two binary trees, root0 and root1, return whether their structure and values are equal. Constraints n ≤ 100,000 where n is the number of nodes in root0 m ≤ 100,000 where m is the number of nodes in root1 Example 1 Input root0 = [0, [5, null, null], [9, null, null]] root1 = [0, [5, null, null], [9, null, null]] Output True Explanation These two trees have the same values and same structure. Example 2 Input root0 = [0, [5, null, null], [9, null, null

View Solution →

Univalue Tree Count - Amazon Top Interview Questions

A univalue tree is a tree where all nodes under it have the same value. Given a binary tree root, return the number of univalue subtrees. Constraints 1 ≤ n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [0, [1, null, null], [0, [1, [1, null, null], [1, null, null]], [0, null, null]]] Output 5 Explanation The unival trees include four leaf nodes (three of them are 1s, the other one is the rightmost 0), and one subtree in the middle (containin

View Solution →

Zero Matrix - Amazon Top Interview Questions

Given a two-dimensional matrix of integers, for each zero in the original matrix, replace all values in its row and column with zero, and return the resulting matrix. Constraints n * m ≤ 100,000 where n and m are the number of rows and columns in matrix Example 1 Input matrix = [ [5, 0, 0, 5, 8], [9, 8, 10, 3, 9], [0, 7, 2, 3, 1], [8, 0, 6, 7, 2], [4, 1, 8, 5, 10] ] Output [ [0, 0, 0, 0, 0], [0, 0, 0, 3, 9], [0, 0, 0, 0, 0], [0,

View Solution →

Subtree with Maximum Average- Amazon Top Interview Questions

Given a binary tree root, return the maximum average value of a subtree. A subtree is defined to be some node in root including all of its descendants. A subtree average is the sum of the node values divided by the number of nodes. Constraints 1 ≤ n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, [3, null, null], [7, [4, null, null], null]] Output 5.5 Explanation The subtree rooted at 7 has the highest average with (7 + 4) / 2. Example 2

View Solution →