Code Posted at ->

Working Java code for each of these puzzles available at http://datastructure-puzzles-codesolutions.blogspot.com


Code Downloads from github https://github.com/inders/datastructure-repo

Saturday, April 2, 2011

Design an efficent structure to store an orginizational hirearchy

You'll have someone starting from CEO, CTO, and multiple levels
Your data structure should be able to support the following operations efficiently

addInvididualContributor()
addManager()
deleteManager()
deleteIndividualContributor()
getEmployeeCountUnderAnEmployee()
getLeastCommonAncestorAcrossTwoEmployees()

4 comments:

Rochak said...

I believe a B-tree should support the requirement of an efficient data structure. Each node will represent a person at a specific organization level. This tree will be sparse towards the root and will become more dense towards its leaves because in a typical organization, lesser people are at the top of the hierarcy (e.g CEO, MD) and more people are at the bottom of the hierarcy)

Rochak said...

The data structure can be roughly like this (in C language):

struct person
{
int EmpID;
char* name;
char* designation;
int numbelow; // no of people directly managed by this person
person* next[];
}

void addperson(int bossID, person* newperson);

void deleteperson(int personID);
void promote(int personID);

Sai Krishna Nallapaneni said...

Rochak's structure looks good except that it needs a manager field. Else it will be a useless search from top trying to figure out the boss in case of addManager() and deleteManager() calls.

Inder said...

1.along with a tree to store the hirearchy you could have a map with pointers to nodes in tree to make the find operation faster.