Amazon interview prep
11 practice questions on the topics candidates reported from Amazon interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
DSA 11
-
An equilibrium index is a position where the sum of the elements before it equals the sum of the elements after it. Which index, counting from 0, is the equilibrium index of [1, 3, 5, 2, 2]?
- 1
- 2
- 3
- There is none
Show the answer
B. 2
At index 2 the left sum is 1 + 3 = 4 and the right sum is 2 + 2 = 4. To find it in O(n), take the total once, then walk the array keeping the left sum: the right sum at i is total - left - a[i].
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
Remove any 3 equal letters that stand next to each other, and repeat until no such run is left. What remains of "aabbbacd"?
- cd
- aacd
- acd
- aaacd
Show the answer
A. cd
Removing "bbb" brings the two a's next to the third, making "aaacd", and removing "aaa" leaves "cd". A stack of (letter, count) pairs does it in one pass: add to the top count when the letter matches, and pop when the count reaches 3.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
In a binary tree, when are two nodes cousins?
- They are at the same depth but have different parents
- They have the same parent
- One is the parent of the other
- They are both leaves
Show the answer
A. They are at the same depth but have different parents
Same depth, different parents. Nodes with the same parent are siblings. A level-order traversal that records each node's depth and parent checks it in O(n).
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
A frog starts on stone 0 and must reach the last stone. From stone i it can jump to stone i + 1 or i + 2, paying |h[i] - h[j]| to land on stone j. The heights are [10, 20, 30, 10]. What is the least total cost?
- 30
- 40
- 20
- 10
Show the answer
C. 20
dp[i] = min(dp[i-1] + |h[i] - h[i-1]|, dp[i-2] + |h[i] - h[i-2]|), giving dp = [0, 10, 20, 20]: jump 0 -> 1 -> 3 for 10 + 10. Each stone needs only the two before it, so O(n) time and O(1) space.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
Does a word appear in a grid of letters, moving up, down, left or right and using each cell at most once? Which approach fits?
- Sort the letters of the grid and binary search the word
- One breadth-first search from the top-left cell
- Count the letters of the word in the grid
- Depth-first search with backtracking, starting from every cell that matches the first letter
Show the answer
D. Depth-first search with backtracking, starting from every cell that matches the first letter
From a matching cell, try the four neighbours for the next letter. Mark the cell as used on the way in and unmark it on the way out, so another path can still use it. Counting letters ignores where they are.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
Each node of a binary tree has a next pointer that should point to the node on its right at the same level, or null for the last node of a level. Which traversal makes this simplest?
- Inorder traversal
- Postorder traversal
- Level-order (breadth-first) traversal
- Reverse inorder traversal
Show the answer
C. Level-order (breadth-first) traversal
Level order visits each level left to right, so each node's next is just the node visited after it in the same level. For a perfect binary tree it can even be done with O(1) extra space by walking the next pointers of the level above.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2023, on GeeksforGeeks.
-
The diameter of a binary tree is the number of edges on the longest path between any two nodes. A root has a left child L and a right child R, and L has a left child LL. What is the diameter?
- 2
- 1
- 4
- 3
Show the answer
D. 3
The longest path is LL - L - root - R, which has 3 edges. In general, for each node take (height of left subtree) + (height of right subtree) and keep the maximum, all in one O(n) traversal.
This topic is named in a candidate’s interview report: SDE-1 interview, 2025, on Reddit.
-
To group words that are anagrams of each other, such as eat, tea and ate, what makes a good hash map key?
- The word's length
- The word itself
- The first letter of the word
- The word's letters sorted, such as "aet", or a count of each letter
Show the answer
D. The word's letters sorted, such as "aet", or a count of each letter
Anagrams share the same multiset of letters, so the sorted letters (O(k log k)) or a 26-letter count (O(k)) is identical for all of them. Words with the same key go in the same group.
This topic is named in a candidate’s interview report: SDE-1 new grad interview, 2024, on Reddit.
-
An LFU cache evicts the least frequently used key, breaking ties by the least recently used. What gives O(1) get and put?
- A single sorted array of keys by frequency
- A binary search tree ordered by frequency, which is O(1)
- A hash map from key to node, a hash map from frequency to a doubly linked list of keys with that frequency, and the current minimum frequency
- Scanning every key to find the least used one
Show the answer
C. A hash map from key to node, a hash map from frequency to a doubly linked list of keys with that frequency, and the current minimum frequency
A hit moves the key from list f to list f + 1. Each list is in recency order, so evicting takes the oldest key in the min-frequency list, and min resets to 1 when a new key arrives.
This topic is named in a candidate’s interview report: SDE-1 new grad interview, 2024, on Reddit.
-
How do you deep-copy a connected graph whose nodes may have cycles?
- Copy each node and its neighbour list as given, sharing the old neighbours
- Sort the nodes first
- Copy only the starting node
- Traverse with BFS or DFS, keeping a map from each original node to its copy; create a copy the first time a node is seen and reuse it after that
Show the answer
D. Traverse with BFS or DFS, keeping a map from each original node to its copy; create a copy the first time a node is seen and reuse it after that
The map is what stops cycles from recursing forever and makes every edge point at the copies. Each node and edge is handled once: O(V + E).
This topic is named in a candidate’s interview report: SDE-1 interview, 2025, on Reddit.
-
What is the smallest palindrome larger than 12345?
- 13331
- 12321
- 12421
- 12345
Show the answer
C. 12421
Mirroring the left half gives 12321, which is smaller than 12345, so increase the middle digit and mirror again: 12421. The general method mirrors, then increments the middle with carries if needed.
This topic is named in a candidate’s interview report: SDE-1 interview, 2025, on Reddit.