Cousins in Binary Tree II
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA reported in July 2026 hands you Cousins in Binary Tree II, and the first attempt usually dies on one detail: subtracting the wrong thing. You've got a level-order string array, "null" markers to preserve, and a rule that siblings don't count as cousins. It's a tree problem, and a level-by-level traversal cracks it. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time. Know the trick before you need it.
The problem
You are given a non-empty binary tree serialized as a level-order string array levelOrder. Each non-null token is a decimal integer, and "null" denotes a missing child. Replace every node's value with the sum of the original values of all its cousins. Two nodes are cousins when they are at the same depth and have different parents. If a node has no cousins, its replacement value is 0. All replacements are conceptually simultaneous. Return the updated tree using the same level-order array shape, preserving every "null" marker from the input. Function replaceValueInTree(levelOrder: String[]) → String[] Examples Example 1 levelOrder = ["5","4","9","1","10","null","7"] return = ["0","0","0","7","7","null","11"] At depth 2, nodes 1 and 10 are siblings, so their only cousin has value 7. Node 7 has cousins with original values 1 and 10, whose sum is 11. Example 2 levelOrder = ["3","1","2"] return = ["0","0","0"] The root has no cousins, and the two nodes at depth 1 are siblings, so every replacement is 0. Example 3 levelOrder = ["1","2","3","4","null","5","6"] return = ["0","0","0","11","null","4","4"] At depth 2, node 4 receives 5 + 6 = 11, while sibling nodes 5 and 6 each receive the cousin value 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two passes over each level. First compute the total of original values at that depth. Then for every node, its new value is the level total minus the sum of its own sibling group, meaning itself plus its sibling if one exists. The common pitfall is subtracting only the node's own value, which leaves the sibling's value counted as a cousin. Example 1 shows it: node 7 gets 1 + 10 = 11, while 1 and 10 each get 7. Another pitfall is mutating values while you still need the originals, so read sums from the old values first. Parsing is the other trap: rebuild the tree from the array, run BFS, then serialize in the same shape with every "null" preserved. If the parsing code slips under pressure, StealthCoder is the hedge for the live OA. Depth 0 and depth 1 always return 0.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Cousins in Binary Tree II cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Cousins in Binary Tree II FAQ
What's the trick in Cousins in Binary Tree II?+
Compute each level's total of original values, then subtract the node's sibling-group sum, which is the node plus its sibling if present. Siblings share a parent, so they aren't cousins. One BFS pass computes level sums, and a second pass assigns the new values.
How hard is this problem really?+
Medium. The algorithm is short once you see the level-sum minus sibling-sum idea. The extra work in this Amazon version is parsing the level-order string array into a tree and serializing it back with the "null" markers kept in place.
What's the most common mistake?+
Subtracting only the node's own value from the level sum. That counts the sibling as a cousin and breaks Example 1, where nodes 1 and 10 should each get 7. You must subtract the whole sibling group's sum, including the node itself.
How do I handle the null markers in the output?+
Keep the original array's shape. Rebuild the tree, update values, then walk it in the same level-order and emit "null" wherever the input had it. Easier still, update values by array index so nulls never move.
How do I prepare in 48 hours?+
Write the BFS level-sum version once from scratch, then test it on the three examples including the all-zero case. Practice parsing level-order arrays into nodes, since that's where candidates lose time. Also know the depth 0 and depth 1 results are always 0.