fork(1) download
  1. #include<iostream>
  2. #include<vector>
  3. #include<unordered_map>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. /*
  9. U have an organizational structure, which shows hierarchy of the organization. This hierarchy contains employees
  10. E or managers M who has some Employees or Managers reporting to M.
  11. Employee has ( id, name, JobDesc, salary etc).
  12. Design the data structure you would be using to store this hierarchy
  13.  
  14. problem 1: Given an ID of an employee , print all the employee ID's who are directly reporting or indirectly
  15. reporting to the manager.
  16.  
  17. problem 2: prefix search of employees by String. If employees have nishant and nikhil. If searched by "ni" we need to print all details of both nishant and nikhil.
  18.  
  19. problem 3(bonus): search should print all emloyee's and their details if a given string is subString of the name of
  20.  an employee.(Like a phonebook contacts search)
  21. */
  22.  
  23.  
  24. class Employee
  25. {
  26. private:
  27. int id;
  28. string name;
  29. string jobDesc;
  30. double sal;
  31. int managerId;
  32. vector<Employee*> reportedEmployees;
  33. public:
  34. int getId()
  35. {
  36. return id;
  37. }
  38.  
  39. void setId(int id)
  40. {
  41. this->id = id;
  42. }
  43.  
  44. string getName()
  45. {
  46. return name;
  47. }
  48.  
  49. void setName(string name)
  50. {
  51. this->name = name;
  52. }
  53.  
  54. string getJobDesc()
  55. {
  56. return jobDesc;
  57. }
  58.  
  59. void setJobDesc(string jobDesc)
  60. {
  61. this->jobDesc = jobDesc;
  62. }
  63.  
  64. double getSal()
  65. {
  66. return sal;
  67. }
  68.  
  69. void setSal(double sal)
  70. {
  71. this->sal = sal;
  72. }
  73.  
  74. int getManagerId()
  75. {
  76. return managerId;
  77. }
  78.  
  79. void setManagerId(int managerId)
  80. {
  81. this->managerId = managerId;
  82. }
  83.  
  84. vector<Employee*>& getReportedEmployees()
  85. {
  86. // cout<< "In getReportedEmployees() empty - "<<reportedEmployees.empty()<<endl;
  87. return reportedEmployees;
  88. }
  89.  
  90. void setReportedEmployees(vector<Employee*> *reportedEmployees)
  91. {
  92. this->reportedEmployees = *reportedEmployees;
  93. }
  94. };
  95.  
  96. class OrganizationStructure
  97. {
  98. public:
  99.  
  100. unordered_map<int, Employee*> data ;
  101.  
  102. void searchPrefixNames(string str)
  103. {
  104. for (unordered_map<int, Employee*>::iterator it=data.begin(); it!=data.end(); ++it)
  105. {
  106. if (it->second->getName().substr(0,str.size())== str)
  107. {
  108. cout<<it->second->getId();
  109. cout<<" "<< it->second->getName();
  110. cout<<" "<< it->second->getJobDesc();
  111. cout<<" "<< it->second->getSal()<<endl;
  112. }
  113. }
  114. }
  115.  
  116. void searchSubNames(string str)
  117. {
  118. for (unordered_map<int, Employee*>::iterator it=data.begin(); it!=data.end(); ++it)
  119. {
  120. if (it->second->getName().find(str)!=string::npos)
  121. {
  122. cout<<it->second->getId();
  123. cout<<" "<< it->second->getName();
  124. cout<<" "<< it->second->getJobDesc();
  125. cout<<" "<< it->second->getSal()<<endl;
  126. }
  127. }
  128. }
  129.  
  130. unordered_map<int, Employee*> storeData(Employee *emp, int parseInt)
  131. {
  132. data.insert ( std::pair<int,Employee*>(parseInt,emp) );
  133.  
  134. Employee *empl;
  135.  
  136. if(data.find(emp->getManagerId()) != data.end())
  137. empl = data.find(emp->getManagerId())->second; /*empl is manager of emp */
  138.  
  139. if(empl->getId()!=emp->getId())
  140. empl->getReportedEmployees().push_back(emp); /* Add emp to reported list of empl */
  141.  
  142. //cout<<"After inserting reportee empty " << empl->getReportedEmployees().empty()<<endl;
  143.  
  144. return data;
  145. }
  146.  
  147. vector<int> getReportedEmployees(int id, unordered_map<int, Employee*> data)
  148. {
  149. vector<int> empListIds;
  150. Employee *emp;
  151.  
  152. if(data.find(id)!=data.end())
  153. {
  154. emp = data.find(id)->second;
  155. }
  156.  
  157. recursiveMethod(data, empListIds, emp->getReportedEmployees());
  158.  
  159. return empListIds;
  160. }
  161.  
  162. void recursiveMethod(unordered_map<int, Employee*> data, vector<int>& empListIds, vector<Employee*> listEmp)
  163. {
  164. std::vector<Employee*>::iterator itr = listEmp.begin();
  165.  
  166. cout<<"listEmp size is - "<<listEmp.size()<<endl;
  167. for (itr=listEmp.begin();itr!=listEmp.end();itr++)
  168. {
  169. // cout<<"in rec method"<<endl;
  170. empListIds.push_back((*itr)->getId());
  171.  
  172. if(data.find((*itr)->getId())!=data.end())
  173. {
  174. //cout<<"data find"<<endl;
  175. Employee *emp;
  176. emp = data.find((*itr)->getId())->second;
  177.  
  178. if(emp->getReportedEmployees().empty()==false)
  179. {
  180. // cout<<"recursive method called"<<endl;
  181. recursiveMethod(data, empListIds, emp->getReportedEmployees());
  182. }
  183. }
  184. }
  185. }
  186. };
  187.  
  188. int main()
  189. {
  190. OrganizationStructure os;
  191.  
  192. Employee *emp = new Employee;
  193. Employee *emp1 = new Employee;
  194. Employee *emp2 = new Employee;
  195. Employee *emp3 = new Employee;
  196. Employee *emp4 = new Employee;
  197. Employee *emp5 = new Employee;
  198. Employee *emp6 = new Employee;
  199. Employee *emp7 = new Employee;
  200. Employee *emp8 = new Employee;
  201. Employee *emp9 = new Employee;
  202. Employee *emp10 = new Employee;
  203.  
  204. vector<Employee*> *vec = new vector<Employee*>;
  205. vector<Employee*> *vec1 = new vector<Employee*>;
  206. vector<Employee*> *vec2 = new vector<Employee*>;
  207. vector<Employee*> *vec3 = new vector<Employee*>;
  208. vector<Employee*> *vec4 = new vector<Employee*>;
  209. vector<Employee*> *vec5 = new vector<Employee*>;
  210. vector<Employee*> *vec6 = new vector<Employee*>;
  211. vector<Employee*> *vec7 = new vector<Employee*>;
  212. vector<Employee*> *vec8 = new vector<Employee*>;
  213. vector<Employee*> *vec9 = new vector<Employee*>;
  214. vector<Employee*> *vec10 = new vector<Employee*>;
  215. unordered_map<int, Employee*> data;
  216.  
  217. emp10->setId(10);
  218. emp10->setName("Praveen");
  219. emp10->setJobDesc("SDE-2");
  220. emp10->setSal(99000.00);
  221. emp10->setReportedEmployees(vec10);
  222. data = os.storeData(emp10, 10);
  223.  
  224. emp->setId(5);
  225. emp->setName("Neeraj");
  226. emp->setJobDesc("SDE-1");
  227. emp->setSal(85000.00);
  228. emp->setReportedEmployees(vec);
  229. emp->setManagerId(10);
  230. data = os.storeData(emp, 5);
  231.  
  232. emp1->setId(12);
  233. emp1->setName("sachin");
  234. emp1->setJobDesc("SME");
  235. emp1->setSal(103000.00);
  236. emp1->setManagerId(5);
  237. emp1->setReportedEmployees(vec1);
  238. data = os.storeData(emp1, 12);
  239.  
  240. emp2->setId(1);
  241. emp2->setName("Ajay");
  242. emp2->setJobDesc("SPM");
  243. emp2->setSal(196000.00);
  244. emp2->setManagerId(5);
  245. emp2->setReportedEmployees(vec2);
  246. data = os.storeData(emp2, 1);
  247.  
  248. emp3->setId(2);
  249. emp3->setName("Anjali");
  250. emp3->setJobDesc("Manager");
  251. emp3->setSal(106000.00);
  252. emp3->setManagerId(5);
  253. emp3->setReportedEmployees(vec3);
  254. data = os.storeData(emp3, 2);
  255.  
  256. emp4->setId(3);
  257. emp4->setName("garima");
  258. emp4->setJobDesc("CA");
  259. emp4->setSal(56010.00);
  260. emp4->setManagerId(5);
  261. emp4->setReportedEmployees(vec4);
  262. data = os.storeData(emp4, 3);
  263.  
  264. emp5->setId(4);
  265. emp5->setName("Sanjeev");
  266. emp5->setJobDesc("IAS");
  267. emp5->setSal(76000.00);
  268. emp5->setManagerId(5);
  269. emp5->setReportedEmployees(vec5);
  270. data = os.storeData(emp5, 4);
  271.  
  272. emp6->setId(6);
  273. emp6->setName("Rajiv");
  274. emp6->setJobDesc("SET");
  275. emp6->setSal(66100.00);
  276. emp6->setManagerId(4);
  277. emp6->setReportedEmployees(vec6);
  278. data = os.storeData(emp6, 6);
  279.  
  280. emp7->setId(7);
  281. emp7->setName("Rajat");
  282. emp7->setJobDesc("SE");
  283. emp7->setSal(36000.00);
  284. emp7->setManagerId(4);
  285. emp7->setReportedEmployees(vec7);
  286. data = os.storeData(emp7, 7);
  287.  
  288. emp8->setId(8);
  289. emp8->setName("Prajakata");
  290. emp8->setJobDesc("SDET-2");
  291. emp8->setSal(67000.00);
  292. emp8->setManagerId(4);
  293. emp8->setReportedEmployees(vec8);
  294. data = os.storeData(emp8, 8);
  295.  
  296. emp9->setId(9);
  297. emp9->setName("Pranay");
  298. emp9->setJobDesc("SDET");
  299. emp9->setSal(36910.00);
  300. emp9->setManagerId(8);
  301. emp9->setReportedEmployees(vec9);
  302. data = os.storeData(emp9, 9);
  303.  
  304.  
  305. vector<int> empListIds = os.getReportedEmployees(10, data);
  306.  
  307. std::vector<int>::iterator itr;
  308. for (itr=empListIds.begin();itr!=empListIds.end();itr++)
  309. cout<<*itr<<" ";
  310.  
  311.  
  312.  
  313. cout<<endl;
  314. os.searchPrefixNames("Pra");
  315. os.searchSubNames("eer");
  316.  
  317. return 0;
  318. }
  319.  
Runtime error #stdin #stdout #stderr 0s 3288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
*** Error in `./prog': free(): invalid pointer: 0x090982a4 ***