Find Minimum Diameter After Merging Two Trees
A hard-tier problem at 57% community acceptance, tagged with Tree, Depth-First Search, Breadth-First Search. Reported in interviews at ServiceNow and 0 others.
You're given two trees and need to connect them with a single edge while minimizing the resulting diameter. ServiceNow asks this one. The trap is thinking you can just pick any nodes to merge and call it done. The real work is computing the diameter of each tree independently, then testing all possible merge points to find the best connection. Most candidates run out of time on the diameter calculation itself or fail to realize they need to evaluate multiple merge strategies, not just one greedy choice. If this lands on your live assessment and you freeze on the merge logic, StealthCoder surfaces a working solution in seconds.
Companies that ask "Find Minimum Diameter After Merging Two Trees"
Find Minimum Diameter After Merging Two Trees is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe algorithm has two phases. First, find the diameter and radius of each tree using two BFS passes per tree: one to find a peripheral node, one from that node to find the farthest point. The diameter is the longest path in the tree. Second, when you merge the trees by connecting two nodes, the new diameter is the maximum of three values: the original diameter of tree one, the original diameter of tree two, or a new path that runs through your merge edge. That new path's length is roughly half each tree's diameter plus one. Most failures happen because candidates either miscalculate the original diameters, don't correctly compute the new diameter formula when merging, or try to merge at random nodes instead of testing the optimal candidates (typically nodes that minimize the radius). Graph and Depth-First Search help you traverse efficiently; Breadth-First Search is critical for finding diameters. With a 57% acceptance rate, this punishes incomplete implementations. StealthCoder is the insurance policy if you hit it live and can't nail the merge formula under time pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find Minimum Diameter After Merging Two Trees recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Minimum Diameter After Merging Two Trees interview FAQ
What's the trick to finding diameter quickly?+
Run BFS twice. First BFS from any node gives you a node at one end of a diameter. Second BFS from that node gives you the farthest node and the actual diameter value. Two passes per tree beats DFS recursion for clarity and speed on a live OA.
Do I really need to test all possible merge nodes?+
You don't need all pairs, but you do need to test candidate nodes that minimize the resulting diameter. For each tree, identify nodes that balance the tree radius well. Testing roughly O(n) candidates is necessary; a greedy single choice will fail.
How do I compute the diameter after merging?+
The new diameter is the max of: diameter of tree one, diameter of tree two, and a path that crosses your merge edge. That crossing path length is roughly ceiling(diameter1/2) plus ceiling(diameter2/2) plus one (the merge edge).
Is this still asked at ServiceNow or companies in general?+
ServiceNow is tagged in the data for this problem. It's a hard-tier problem, so it appears in senior or technical loop rounds, not entry-level screens. Lower frequency than medium trees, but if you see it, you're in a serious conversation.
How does this relate to standard tree DP or graph traversal?+
It combines tree diameter (a classic DP problem) with graph merging logic. You need depth-first and breadth-first search comfort to handle both the traversal and the multi-phase nature of finding diameters and testing merge points.
Want the actual problem statement? View "Find Minimum Diameter After Merging Two Trees" on LeetCode →