Reported September 2026
LinkedInhash table

Merge N-Ary Trees by Key

Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live LinkedIn OA. Under 2s to a working solution.
Founder's read

The LinkedIn OA reported in September 2026 hands you two N-ary trees as parallel key and parent arrays, then asks for a merged tree printed as sorted root-to-node paths. It looks like a tree problem, but it's really a hash map problem wearing a tree costume. The trap is matching keys globally instead of per merged parent. If you blank under the clock, StealthCoder is the invisible safety net that reads the problem and gives you a working solution. Know the shape before you sit down.

The problem

Two immutable rooted N-ary trees are encoded by keys1, parents1, keys2, and parents2. For either tree, keys[i] is the key of node i. The root is node 0 and has parent -1; every later node has a parent index smaller than its own index.
Perform one sequential merge. The roots have the same key and become one root. Under two already-merged parent nodes, children with the same key become one child and their descendants are merged by the same rule. A child key that appears under only one of those parents is copied with its entire subtree. Equal keys under different parent paths are different nodes and do not merge.
Return a canonical description of the merged tree as every root-to-node key path, including the root path. Join adjacent keys in a path with /, then return all paths in lexicographic order.

Function
mergeNaryTrees(keys1: String[], parents1: int[], keys2: String[], parents2: int[]) → String[]

Examples
Example 1
keys1 = ["a","b","d","c"]
parents1 = [-1,0,1,0]
keys2 = ["a","b","e","d"]
parents2 = [-1,0,1,0]
return = ["a","a/b","a/b/d","a/b/e","a/c","a/d"]
The two a/b nodes merge because both their parent path and key match. Their children d and e are both retained. The root children c and d occur in only one input tree.
Example 2
keys1 = ["r","a","x","b","k"]
parents1 = [-1,0,1,0,3]
keys2 = ["r","a","k","b","x"]
parents2 = [-1,0,1,0,3]
return = ["r","r/a","r/a/k","r/a/x","r/b","r/b/k","r/b/x"]
Keys k and x each occur below both a and b across the two trees. They stay separate because matching is relative to already-merged parent nodes.

Constraints
1 <= keys1.length, keys2.length <= 2000
keys1.length == parents1.length and keys2.length == parents2.length.
Each key contains only lowercase English letters and has length from 1 through 20.
For each tree, parents[0] == -1, and 0 <= parents[i] < i for every i > 0.
Children of the same parent have distinct keys.
keys1[0] == keys2[0].
The total length of all returned path strings fits in memory.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build children maps for each tree: node index to a map of key to child index. Then recurse from the two roots together. At each merged pair, take the union of child keys. If a key exists in both, recurse with both child indices. If it exists in only one, recurse with the other side set to -1 so the whole subtree gets copied. Build the path string as you go and add it to a result list. Sort at the end. The edge case that breaks naive solutions is example 2: keys k and x appear under both a and b, and a global key-to-node map would wrongly fuse them. Matching must be relative to the merged parent, never by key alone. Depth can reach 2000, so watch recursion limits or go iterative. StealthCoder is your hedge on the live OA if the recursion with a missing side gets tangled.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Merge N-Ary Trees by Key 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass LinkedIn's OA.

LinkedIn reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Merge N-Ary Trees by Key FAQ

What's the trick in the LinkedIn merge N-ary trees problem?+

Match children by key only within an already-merged parent pair. Build a key-to-child map per node, walk both trees together, and take the union of keys at each step. A global key lookup is the classic wrong answer.

How do I handle a child that exists in only one tree?+

Recurse with the other tree's node set to -1 (or null). Every child of the present side then counts as unmatched and gets copied. That copies the full subtree without a separate copy routine.

Do I need to build an actual tree to solve this?+

Not really. The parent arrays let you build children maps directly, and you only need to emit path strings. You can skip node objects and carry the two indices plus the current path string through the recursion.

How should I sort the output paths?+

Collect every path string, then do a plain lexicographic sort at the end. Paths use / as the separator, so sort the full joined strings as the problem states. Don't rely on traversal order to produce sorted output.

How do I prepare for this in 48 hours?+

Write the merge once by hand: children maps, a recursive walk with two indices, and a path list. Test it against example 2, which catches the cross-parent key bug. Also check depth. With up to 2000 nodes, a deep chain can overflow recursion in some languages.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with LinkedIn.

OA at LinkedIn?
Invisible during screen share
Get it