Pick the Root Servers That Minimize Replication Tree Height

Last updated: November 29, 2026

Quick Overview

n servers are connected as an undirected tree of replication links. Return every server that, chosen as the root, gives a tree of minimum height.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


n servers are connected as an undirected tree of replication links. Return every server that, chosen as the root, gives a tree of minimum height.

Minimum height trees in the style of Meta's onsite coding rounds, framed around choosing the primary for a replication tree so the slowest replica is as few hops away as possible. The interviewer checks that you avoid BFS from every node and instead peel leaves layer by layer toward the center.

What the Interviewer Expects
  • Rejects BFS from every node as O(n^2)
  • Observes the answer is the center of the longest path, so one or two nodes
  • Peels leaves layer by layer using degrees
  • Stops when two or fewer nodes remain
  • Handles n = 1 and n = 2
Key Topics to Cover
Trees
BFS
Topological peeling
Graph center
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Prove that there are at most two centers.
  • How would you find the center using two BFS passes for the diameter instead?
  • How would the answer change if links had latencies?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: n and links, n - 1 undirected edges forming a tree. Output: list of root candidates with minimal height, any order. n up to 2 * 10^4....

High-Level Design

The height from a root is its distance to the farthest leaf, which is minimized at the middle of the tree's longest path. Peeling finds that middle without computing the diameter: remove all current l...


Submit Your Answer
Markdown supported