title-img


First Missing Positive - Amazon Top Interview Questions

Given a list of integers nums, find the first missing positive integer. In other words, find the lowest positive integer that does not exist in the list. The list can contain duplicates and negative numbers as well. Constraints n ≤ 100,000 where n is the length of nums. Example 1 Input nums = [1, 2, 3] Output 4 Example 2 Input nums = [3, 4, -1, 1] Output 2 Example 3 Input nums = [1, 2, 0] Output 3 Example 4 Input nums = [-1, -2, -3]

View Solution →

Height Balanced Tree - Amazon Top Interview Questions

Given the root of a binary tree, return whether its height is balanced. That is, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or 1. Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, null, [4, null, [12, null, null]]] Output False Explanation This is false since the root's right subtree has height of 2, and left has height of 0. Example 2 Input

View Solution →

Inorder Traversal - Amazon Top Interview Questions

Given a binary tree root, return an inorder traversal of root as a list. Bonus: Can you do this iteratively? Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, null, [159, [80, null, null], null]] Output [1, 80, 159]

View Solution →

Invert Tree - Amazon Top Interview Questions

Given a binary tree root, invert it so that its left subtree and right subtree are swapped and the children are recursively inverted. Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [0, [2, null, null], [9, [7, null, null], [12, null, null]]] Output [0, [9, [12, null, null], [7, null, null]], [2, null, null]]

View Solution →

Island Shape Perimeter - Amazon Top Interview Questions

Given a two-dimensional integer matrix of 1s and 0s where 0 represents empty cell and 1 represents a block that forms a shape, return the perimeter of the shape. There is guaranteed to be exactly one shape, and the shape has no holes in it. Constraints 1 ≤ n, m ≤ 250 where n and m are the number of rows and columns in matrix Example 1 Input matrix = [ [0, 1, 1], [0, 1, 0] ] Output 8

View Solution →