Closest Shared Manager of Two Employees in an Org Chart

Last updated: November 29, 2026

Quick Overview

Given an org chart as a map from each manager to their direct reports, the root (CEO), and two employee names, return the lowest manager who has both employees in their reporting chain (an employee counts as in their own chain), or None if either is absent.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an org chart as a map from each manager to their direct reports, the root (CEO), and two employee names, return the lowest manager who has both employees in their reporting chain (an employee counts as in their own chain), or None if either is absent.

Framed around Google's internal directory (Teams or Moma style org lookups), this is lowest common ancestor on an n-ary tree. The interviewer checks that you handle the case where one employee manages the other and the case where an employee does not exist, which the classic binary-tree recursion gets wrong.

What the Interviewer Expects
  • Generalizes LCA from binary trees to n-ary trees
  • Handles one employee being an ancestor of the other
  • Correctly returns None when an employee is missing
  • Avoids recursion depth issues or mentions them for deep orgs
  • Discusses answering many queries quickly
Key Topics to Cover
Trees
Lowest Common Ancestor
DFS
Recursion
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
  • Answer a million queries on a fixed org chart quickly.
  • Each employee has a parent pointer only; find the LCA without the child map.
  • The org chart changes frequently (reorgs); how do you keep queries fast?
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: reports map (manager to list of direct reports), root, and names p, q.
  • Output: the deepest node whose subtree contains both p and q; None if either is not in the tree.
  • A node is i...
High-Level Design

Do one post-order DFS that returns, for each subtree, how many of {p, q} it contains (0, 1, or 2). The first node (deepest, since post-order) whose count reaches 2 is the answer; record it and stop co...


Submit Your Answer
Markdown supported