Domain 2MediumCORE

Managers with at Least 5 Direct Reports

The goal is to identify employees who act as managers for at least five direct reports. - Inputs: A dataset representing the workforce (typically a list of dictionaries or rows). Each entry represents an employee with unique identifiers and, crucially, information about whom they report to (managerid). If reportsto logic exists in reverse, we look for how many times each ID appears as a manager. - Outputs: A list of IDs corresponding to managers who meet the threshold (>= 5 direct reports). - Constraints/Edge Cases: - Employees at the top level have no manager; they do not count towards anyone's report total, nor are they necessarily filtered out unless we need their own ID. - Null or missing manager_id values should be ignored for counting purposes (they don't belong to a specific manager). - The threshold is inclusive (>= 5).

Domain: Medium•Updated: 2026-08-31
#Algorithms#LeetCode#qwen3.5:9b#FAANG asked: 1

570. Managers with at Least 5 Direct Reports

Model: qwen3.5:9b
Topics:
FAANG asked: 1
Acceptance rate: 66.8%


Problem Breakdown

The goal is to identify employees who act as managers for at least five direct reports.

  • Inputs: A dataset representing the workforce (typically a list of dictionaries or rows). Each entry represents an employee with unique identifiers and, crucially, information about whom they report to (manager_id). If reports_to logic exists in reverse, we look for how many times each ID appears as a manager.
  • Outputs: A list of IDs corresponding to managers who meet the threshold (>= 5 direct reports).
  • Constraints/Edge Cases:
    • Employees at the top level have no manager; they do not count towards anyone’s report total, nor are they necessarily filtered out unless we need their own ID.
    • Null or missing manager_id values should be ignored for counting purposes (they don’t belong to a specific manager).
    • The threshold is inclusive (>= 5).

Intuition

A brute-force approach would iterate through every employee and, for each one, scan the entire list of employees again to count how many report back to them. This results in O(N2)O(N^2) time complexity, which will fail on large datasets (e.g., millions of rows).

The key insight is that we don’t need nested loops; we just need frequency counts. We can iterate through the dataset exactly once and aggregate data into a hash map where keys are manager_id and values are the count of direct reports for that manager. Once aggregated, filtering this dictionary takes O(1)O(1) per entry relative to N (linear scan).

Approach

  1. Initialize Storage: Create an empty dictionary (counts) to store the frequency of each manager_id.
  2. Aggregate Data: Iterate through every employee record in the input list. For each record, extract their manager’s ID. If a valid manager exists (not null/None), increment that manager’s count in our map.
  3. Filter Results: After processing all records, iterate through the dictionary keys and collect those where the value is greater than or equal to 5.
  4. Return Output: Return the list of qualifying manager_ids (or tuples if names are required).

Complexity Analysis

  • Time Complexity: O(N)O(N), where NN is the number of employees in the dataset. We iterate through the data once for aggregation and once more to filter results (2×N2 \times N, which simplifies to linear time).
  • Space Complexity: O(M)O(M), where MM is the number of unique managers (keys in our dictionary). In the worst case, every employee reports to a different manager, so space scales with input size.

Solution

from typing import List, Dict, Optional

def find_managers(employees: List[Dict[str, int]]) -> List[int]:
    """
    Identifies managers who have at least 5 direct reports.
    
    Args:
        employees: A list of dictionaries representing employee records. 
                   Each dict should contain 'manager_id' (int or None).
                   
    Returns:
        A list of manager IDs meeting the threshold.
    """
    # Dictionary to store count of direct reports per manager ID
    report_counts = {}

    for emp in employees:
        # Extract manager ID; handle cases where an employee has no manager (None/null)
        mgr_id = emp.get('manager_id')
        
        if mgr_id is not None and mgr_id != 0: 
            # Increment count for this specific manager
            report_counts[mgr_id] = report_counts.get(mgr_id, 0) + 1

    # Filter managers who have at least 5 direct reports
    return [mid for mid, count in report_counts.items() if count >= 5]

Edge Cases Handled

  • No Managers: If the input list is empty or contains no valid manager_ids (all null), the dictionary remains empty and an empty list is returned.
  • Threshold Boundary: A manager with exactly 4 reports returns nothing; a manager with exactly 5 returns their ID. The logic uses >= to handle this correctly.
  • Null/Invalid IDs: Checks ensure that None, 0, or missing keys do not crash the aggregation loop (using .get() and explicit checks).