Longest Reporting Chain Between Any Two People in an Org Tree

Last updated: November 29, 2026

Quick Overview

An org chart is a rooted tree where each manager has any number of reports. Return the number of edges on the longest path between any two people, which may or may not pass through the CEO.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


An org chart is a rooted tree where each manager has any number of reports. Return the number of edges on the longest path between any two people, which may or may not pass through the CEO.

This is tree diameter generalized to n-ary nodes, a common Meta screen problem. The interviewer checks whether you compute height and diameter in one post-order pass, and whether you notice the best path may not pass through the root.

What the Interviewer Expects
  • Recognizes this as tree diameter measured in edges
  • Uses one post-order DFS returning height while updating a global best
  • Keeps only the two tallest child heights per node
  • Points out the path may skip the root
  • Mentions an iterative approach for very deep trees
Key Topics to Cover
Trees
DFS
Post-order traversal
Tree diameter
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
  • How would you also return the two endpoints of the path?
  • What if edges had weights, like time zones crossed?
  • How would you do it iteratively for a chain of a million nodes?
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

Each node has a value and a list of children. Return edge count of the longest path; a single node or empty tree returns 0. The tree can be unbalanced. Target O(n) time....

High-Level Design

For each node, the longest path that bends at that node goes down through its two tallest child subtrees. So a post-order DFS returns the node's height (edges to its deepest descendant) and, along the...


Submit Your Answer
Markdown supported