Design DB schema for manager-employee relationship

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2020-10-14

Problem Statement

Design a system that represents the hierarchy of employees and managers in an organization. The focus is on DB schema design, object-oriented modeling, and support for queries related to hierarchy, reporting, and management.


✅ Functional Requirements


1.Employee Management
•Store employee details: employeeId, name, designation, department, managerId.
•Track reporting structure: each employee reports to one manager (except top-level managers).
•Support CRUD operations on employee records.
2.Manager-Employee Relationship
•Maintain a hierarchical structure: manager → direct reports → indirect reports.
•Support queries for:
•List of direct reports of a manager.
•List of all reports (direct + indirect) of a manager.
•Find manager chain for a given employee.
3.Optional Features
•Support multiple managers per employee (matrix organization).
•Track employee promotions and reporting changes over time.
•Allow department-wise queries.


🔐 Constraints & Assumptions


•Each employee has a unique employeeId.
•Assume a single reporting manager for basic hierarchy.
•Focus is on LLD and schema design, not UI or authentication.


🧱 Key Components & Classes


1.Employee Class
1 2class Employee { 3private: 4 int employeeId; 5 string name; 6 string designation; 7 string department; 8 Employee* manager; // reference to manager 9public: 10 vector<Employee*> getDirectReports(); 11 vector<Employee*> getAllReports(); 12 vector<Employee*> getManagerChain(); 13};
1.Manager Class (Optional)
•In this design, Manager can be a subclass of Employee or simply represented via the manager reference in Employee.
2.Organization Class
1 2class Organization { 3private: 4 unordered_map<int, Employee*> employees; // employeeId -> Employee 5public: 6 void addEmployee(Employee* employee); 7 void removeEmployee(int employeeId); 8 Employee* getEmployee(int employeeId); 9 vector<Employee*> getDirectReports(int managerId); 10};


🔄 Example Workflow


1.Add employees: Alice (Manager), Bob (Reports to Alice), Charlie (Reports to Bob).
2.Retrieve direct reports of Alice → returns [Bob].
3.Retrieve all reports of Alice → returns [Bob, Charlie].
4.Retrieve manager chain of Charlie → returns [Bob, Alice].


🧪 Database Schema


Employee Table


Column Type Description


employee_id INT (PK) Unique employee identifier


name VARCHAR Employee name


designation VARCHAR Job title


department VARCHAR Department name


manager_id INT (FK) References employee.employee_id (NULL if top-level)


Indexes & Constraints


•Primary Key: employee_id
•Foreign Key: manager_id references employee_id
•Index on manager_id for fast retrieval of direct reports


Solution

Comments