Invite the most employees so no one attends with their direct manager

Quick Overview

Given a company hierarchy as a parent array, find the largest number of employees you can invite so that no employee attends together with their direct manager. Tests tree dynamic programming with include and exclude states and an iterative traversal that stays safe on very deep trees.

Invite the most employees so no one attends with their direct manager

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A company hierarchy is a rooted tree with `n` employees numbered `0` to `n - 1`. It is described by an array `parent` of length `n`, where `parent[i]` is the index of the direct manager of employee `i`. Exactly one employee, the CEO, is the root and has `parent[i] == -1`. You must invite a subset of employees to a meeting, under one rule: an employee and their direct manager can never both be invited. Return the maximum number of employees that can be invited. ### Function Signature ```python def max_invitations(n: int, parent: list[int]) -> int: ``` ### Rules - The tree is given only through `parent`. A manager can have any number of direct reports. - The rule applies only to an employee and their direct manager. An employee and their manager's manager may both be invited. - Return the count. Several different sets can reach the maximum, but the count is unique. ### Constraints - `1 <= n <= 10^5` - `parent.length == n` - `parent[i] == -1` for exactly one `i` (the root) - `0 <= parent[i] < n` for every non-root employee - `parent` describes a valid rooted tree ### Examples ```text Input: n = 7, parent = [-1,0,0,1,1,2,2] Output: 5 ``` Employee 0 manages 1 and 2, employee 1 manages 3 and 4, and employee 2 manages 5 and 6. Inviting the four leaves 3, 4, 5, 6 together with the root 0 is valid because none of their direct managers or direct reports (1 and 2) are invited, which gives 5. ```text Input: n = 1, parent = [-1] Output: 1 ``` Only the root exists, so it is invited. ```text Input: n = 4, parent = [-1,0,1,2] Output: 2 ``` The hierarchy is a chain 0, 1, 2, 3, so any two neighbors conflict. At most every other employee can come, for example {0, 2} or {1, 3}.

Overview: Given a company hierarchy as a parent array, find the largest number of employees you can invite so that no employee attends together with their direct manager. Tests tree dynamic programming with include and exclude states and an iterative traversal that stays safe on very deep trees.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A company hierarchy is a rooted tree with n employees numbered 0 to n - 1. It is described by an array parent of length n, where parent[i] is the index of the direct manager of employee i. Exactly one employee, the CEO, is the root and has parent[i] == -1.

You must invite a subset of employees to a meeting, under one rule: an employee and their direct manager can never both be invited.

Return the maximum number of employees that can be invited.

Function Signature

def max_invitations(n: int, parent: list[int]) -> int:

Rules

  • The tree is given only through parent . A manager can have any number of direct reports.
  • The rule applies only to an employee and their direct manager. An employee and their manager's manager may both be invited.
  • Return the count. Several different sets can reach the maximum, but the count is unique.

Constraints

  • 1 <= n <= 10^5
  • parent.length == n
  • parent[i] == -1 for exactly one i (the root)
  • 0 <= parent[i] < n for every non-root employee
  • parent describes a valid rooted tree

Examples

Input: n = 7, parent = [-1,0,0,1,1,2,2]
Output: 5

Employee 0 manages 1 and 2, employee 1 manages 3 and 4, and employee 2 manages 5 and 6. Inviting the four leaves 3, 4, 5, 6 together with the root 0 is valid because none of their direct managers or direct reports (1 and 2) are invited, which gives 5.

Input: n = 1, parent = [-1]
Output: 1

Only the root exists, so it is invited.

Input: n = 4, parent = [-1,0,1,2]
Output: 2

The hierarchy is a chain 0, 1, 2, 3, so any two neighbors conflict. At most every other employee can come, for example {0, 2} or {1, 3}.