Reconstruct the Root Stream
Reported by candidates from Arcesium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Arcesium OA reported in July 2025 hands you a perfect binary tree and asks you to run a data stream backwards. Leaves hold the pieces, internal nodes tell you how the stream was split, and you rebuild the root. It's a tree problem, but the real work is merging two child streams correctly at each node. One wrong assumption about odd lengths and your output is off by a position. If you freeze on the merge logic during the live assessment, StealthCoder runs invisibly on your desktop as a safety net, so you aren't stuck staring at it.
The problem
A perfect binary tree distributes a root data stream to its leaves. The internal nodes appear in level order in distributionNodes: Type 0 is a splitting node. It sends the first half to its left child and the remaining half to its right child. For an odd-sized stream, the left child receives one more item. Type 1 is a parallelizing node. It sends items at positions 0, 2, 4,... to its left child and items at positions 1, 3, 5,... to its right child. streamAtLeaves lists the streams received by the leaves from left to right. Rows may be padded with -1; ignore those padding values. Reconstruct and return the original stream at the root. Function reconstructRootStream(distributionNodes: int[], streamAtLeaves: int[][]) → int[] Examples Example 1 distributionNodes = [0,1,1] streamAtLeaves = [[1,3],[2,4],[5,7],[6,8]] return = [1,2,3,4,5,6,7,8] Each type-1 child pair reconstructs to [1,2,3,4] and [5,6,7,8]. The type-0 root concatenates those halves. Example 2 distributionNodes = [1,0,0] streamAtLeaves = [[10],[20],[30],[40]] return = [10,30,20,40] The type-0 nodes reconstruct [10,20] and [30,40]. Alternating those at the type-1 root gives [10,30,20,40]. Constraints 1 <= distributionNodes.length <= 10^4. Every entry of distributionNodes is 0 or 1. The number of leaf rows equals distributionNodes.length + 1. The total number of non-padding values is at most 10^6. Every stream value is positive; -1 is reserved for padding.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Work bottom-up. Strip the -1 padding from each leaf row, then combine pairs level by level using the node types, which come in level order. Type 0 means the parent is left followed by right, a plain concatenation. Type 1 means the parent interleaves: left item, right item, left item, and so on. The edge case is odd length. In a split, the left child gets the extra item, so the left can be longer than the right by exactly one. When interleaving, you must append the leftover left item at the end, not drop it. Example 2 shows the order matters across levels. A common pitfall is mapping node indices wrong. Process the internal nodes from the last index down to 0, using children 2i+1 and 2i+2 for the heap layout. Total values cap at 10^6, so linear merging per level is fine. StealthCoder is your hedge if the indexing or interleave tail blanks on you mid-assessment.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Reconstruct the Root Stream 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Arcesium's OA.
Arcesium reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Reconstruct the Root Stream FAQ
What's the trick in Reconstruct the Root Stream?+
Go bottom-up. Clean the padding from the leaves, then merge sibling streams into their parent using the parent's type. Type 0 concatenates left then right. Type 1 interleaves, alternating left and right. Repeat until you reach the root node at index 0.
How do I handle odd-length streams?+
For a splitting node, the left child gets the extra item, so left length is right length or right plus one. When reversing, concatenation just works. For interleaving, loop while either side has items and append the left leftover at the end. Don't assume equal lengths.
How do I map nodes to children?+
The internal nodes are given in level order, so treat it as a heap array. Node i has children 2i+1 and 2i+2. Leaves come after the internal nodes, so child indices at or beyond the internal count map to leaf rows at index minus the internal count.
What's the complexity, and will it pass the limits?+
Each level touches every value once, and the tree has about log of the leaf count levels. Total work is roughly values times depth, which is fine for 10^6 values. Avoid repeated list slicing that copies data needlessly. Build new arrays per merge.
How do I prepare in 48 hours for this kind of OA?+
Write the merge function first and test it on both examples by hand. Then wire up the bottom-up loop. Practice a couple of perfect-binary-tree-in-an-array problems so the 2i+1 indexing feels automatic. Check the all-padding and single-node cases before you submit.