title-img


A Flight of Stairs - Amazon Top Interview Questions

There's a staircase with n steps, and you can climb up either 1 or 2 steps at a time. Given an integer n, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters, so each different order of steps counts as a way. Mod the result by 10 ** 9 + 7. Constraints n ≤ 100,000 Example 1 Input n = 4 Output 5 Explanation There are 5 unique ways: 1, 1, 1, 1 2, 1, 1 1, 2, 1 1, 1, 2 2, 2 Example 2

View Solution →

Characters in Each Bracket Depth - Amazon Top Interview Questions

You are a given a string s containing "X", "(", and ")". The string has balanced brackets and in between there are some "X"s along with possibly nested brackets recursively. Return the number of "X"s at each depth of brackets in s, from the shallowest depth to the deepest depth. Constraints 2 ≤ n ≤ 100,000 where n is the length of s Example 1 Input s = "(XX(XX(X))X)" Output [3, 2, 1] Explanation There's three "X"s at depth 0. Two "X"s at depth 1. And one "X" at dept

View Solution →

Dice Throw - Amazon Top Interview Questions

Given integers n, faces, and total, return the number of ways it is possible to throw n dice with faces faces each to get total. Mod the result by 10 ** 9 + 7. Constraints 1 ≤ n, faces, total ≤ 100 Example 1 Input n = 2 faces = 6 total = 7 Output 6 Explanation There are 6 ways to make 7 with 2 6-sided dice: 1 and 6 6 and 1 2 and 5 5 and 2 3 and 4 4 and 3

View Solution →

Back to Front Linked List - Amazon Top Interview Questions

Given a singly linked list node, reorder it such that we take: the last node, and then the first node, and then the second last node, and then the second node, etc. Can you do it in \mathcal{O}(1)O(1) space? Constraints 0 ≤ n ≤ 100,000 where n is the number of nodes in node Example 1 Input node = [0, 1, 2, 3] Output [3, 0, 2, 1]

View Solution →

Binary Tree Width - Amazon Top Interview Questions

Given a binary tree root, return the maximum width of any level in the tree. The width of a level is the number of nodes that can fit between the leftmost node and the rightmost node. Constraints 1 ≤ n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [0, [1, [3, null, null], null], [2, [4, [5, null, null], [6, null, null]], null]] Output 3 Explanation The maximum width is 3 since between nodes 3 and 4, we can fit total of 3 nodes: [3, null, 4]

View Solution →