PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/LinkedIn

Merge Keyed N-Ary Trees

Last updated: Jul 28, 2026

Quick Overview

Merge two keyed N-ary trees into a fresh result while preserving the required child order and matching nodes only within the same parent. Account for unmatched branches, very deep inputs, immutability, complexity, and concurrent-merge follow-ups.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Software Engineer

Merge Keyed N-Ary Trees

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Merge Keyed N-Ary Trees An N-ary tree node contains a unique string `key` among its siblings and a list of children. Implement `merge_trees(a, b)` for two roots with the same key. Nodes at the same parent are merged only when their keys match. Matching nodes are merged recursively. A child present in only one tree is retained unchanged. Preserve the first tree's child order; append children whose keys appear only in the second tree in their original order. The function must return a new tree and must not mutate either input. ## Constraints - Each sibling list contains no duplicate key. - The two root keys are equal. - The total number of nodes across both inputs is at most `200000`. - Tree depth may be large, so identify how recursion depth is handled. ## Example If root `r` has children `[a(children=[x]), b]` in the first tree and `[a(children=[y]), c]` in the second, the merged root has children `[a(children=[x, y]), b, c]`. ## Clarifications Keys define identity only within the same parent. Equal keys in unrelated branches do not merge. Discuss expected time and space complexity and the data structure used to avoid repeatedly scanning sibling lists. ## Hints At each merged parent, an index from child key to output position can turn repeated matching into direct lookup. ## Extensions - How would you merge several trees concurrently without corrupting shared state? - Compare coarse locks, per-node locks, immutable snapshots, and MVCC for a shared version. - How would conflicting payload values be resolved if nodes also stored data?

Quick Answer: Merge two keyed N-ary trees into a fresh result while preserving the required child order and matching nodes only within the same parent. Account for unmatched branches, very deep inputs, immutability, complexity, and concurrent-merge follow-ups.

Related Interview Questions

  • The Celebrity Problem - LinkedIn (medium)
  • Longest Run of Ones After One Flip - LinkedIn (medium)
  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
|Home/Coding & Algorithms/LinkedIn

Merge Keyed N-Ary Trees

LinkedIn logo
LinkedIn
Jun 11, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Merge Keyed N-Ary Trees

An N-ary tree node contains a unique string key among its siblings and a list of children. Implement merge_trees(a, b) for two roots with the same key.

Nodes at the same parent are merged only when their keys match. Matching nodes are merged recursively. A child present in only one tree is retained unchanged. Preserve the first tree's child order; append children whose keys appear only in the second tree in their original order. The function must return a new tree and must not mutate either input.

Constraints

  • Each sibling list contains no duplicate key.
  • The two root keys are equal.
  • The total number of nodes across both inputs is at most 200000 .
  • Tree depth may be large, so identify how recursion depth is handled.

Example

If root r has children [a(children=[x]), b] in the first tree and [a(children=[y]), c] in the second, the merged root has children [a(children=[x, y]), b, c].

Clarifications

Keys define identity only within the same parent. Equal keys in unrelated branches do not merge. Discuss expected time and space complexity and the data structure used to avoid repeatedly scanning sibling lists.

Hints

At each merged parent, an index from child key to output position can turn repeated matching into direct lookup.

Extensions

  • How would you merge several trees concurrently without corrupting shared state?
  • Compare coarse locks, per-node locks, immutable snapshots, and MVCC for a shared version.
  • How would conflicting payload values be resolved if nodes also stored data?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More LinkedIn•More Software Engineer•LinkedIn Software Engineer•LinkedIn Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.