Navigating Organization Hierarchy: A Python Solution for Employee-Manager Relationships

In the dynamic landscape of organizations, understanding the hierarchical structure is pivotal for effective communication and management. This coding challenge presents a scenario where two arrays, one containing employee IDs and the other their corresponding manager IDs, need to be decoded to reveal the organization's hierarchy. This blog post explores a Python solution to this problem, offering insights into the code and its practical application.
Problem Overview:
- Employee IDs Array: An array representing the unique IDs of all employees in the organization.
- Manager IDs Array: A corresponding array where each index contains the manager ID for the employee at the same index.
Key Points:
1. Each employee has a single manager.
2. Each manager is also an employee.
3. Each manager can oversee zero or more employees.
4. Only one employee lacks a manager, identified by a corresponding manager ID of -1.
Solution Approach:
The problem is tackled by creating a dictionary to depict the organizational hierarchy. Each manager becomes a key in the dictionary, with the corresponding value being a list of employees managed by that specific manager. The solution then prints the hierarchy in a structured format, starting from the CEO and traversing through the organization's layers.
Code Breakdown:
The provided Python code follows a logical sequence:
1. Creation of an empty dictionary, `hierarchy`.
2. Iteration through the employee and manager arrays to populate the `hierarchy` dictionary.
3. Identification of the CEO, achieved by finding the employee with a manager ID of -1.
4. Printing of the hierarchy in a structured format, commencing with the CEO and extending to their direct reports.
Example Usage:
```python
employee_ids = [1, 49, 29, 95, 90, 10, 24, 84, 17, 19, 6]
manager_ids = [-1, 17, 6, 49, 29, 1, 6, 29, 10, 6, 1]
print_organization_hierarchy(employee_ids, manager_ids)
Output:
CEO: 1
Direct Reports:
49 6
Direct Reports of 49:
29
Direct Reports of 6:
10 24 19
#HCLCodingInterview#Fresher#Experinced#InterviewPrep#Coding#Softwaredevelopment#Python
Latest News