PornToken

Chris Gu, Coding test successful answer, Initech

Jan 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package lca;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Company {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. /*
  11. * Initech Test Example from PDF
  12. */
  13.  
  14. Employee milton = new Employee(8, "Milton");
  15. Employee nina = new Employee(9, "Nina");
  16. Employee bob = new Employee(5, "Bob");
  17. Employee peter = new Employee(6, "Peter");
  18. Employee porter = new Employee(7, "Porter");
  19. Employee dom = new Employee(2, "DOM");
  20. Employee samir = new Employee(3, "Samir");
  21. Employee michael = new Employee(4, "Michael");
  22. Employee bill = new Employee(1, "Bill");
  23.  
  24. // Fourth Row
  25. peter.addReport(milton);
  26. peter.addReport(nina);
  27.  
  28. // Third Row
  29. dom.addReport(bob);
  30. dom.addReport(peter);
  31. dom.addReport(porter);
  32.  
  33. // Second Row
  34. bill.addReport(dom);
  35. bill.addReport(samir);
  36. bill.addReport(michael);
  37.  
  38. Employee closestMgr = closestCommonManager(bill, milton, porter);
  39.  
  40. System.out.println("Closest Common Manager between Milton and Porter is: " + closestMgr.getName());
  41.  
  42. }
  43.  
  44. // IMPORTANT: DO NOT MODIFY THIS CLASS
  45. public static class Employee {
  46.  
  47. private final int id;
  48. private final String name;
  49. private List<Employee> reports;
  50.  
  51. public Employee(int id, String name) {
  52. this.id = id;
  53. this.name = name;
  54. this.reports = new ArrayList<Employee>();
  55. }
  56.  
  57. public int getId() {
  58. return id;
  59. }
  60.  
  61. public String getName() {
  62. return name;
  63. }
  64.  
  65. public List<Employee> getReports() {
  66. return reports;
  67. }
  68.  
  69. public void addReport(Employee employee) {
  70. reports.add(employee);
  71. }
  72. }
  73.  
  74. /*
  75. * Read the attached PDF for more explanation about the problem
  76. * Note: Don't modify the signature of this function
  77. * @param ceo
  78. *
  79. * @param firstEmployee
  80. *
  81. * @param secondEmployee
  82. *
  83. * @return common manager for both employees that is closest to them.
  84. */
  85.  
  86. public static Employee closestCommonManager(Employee ceo, Employee firstEmployee, Employee secondEmployee) {
  87. // Implement me
  88.  
  89. /*
  90. * This is a Office Space version of the
  91. * k-ary tree lowest common ancestor algorithm
  92. */
  93.  
  94. // immediate match found
  95. if(firstEmployee == ceo || secondEmployee == ceo) {
  96. return ceo;
  97. }
  98.  
  99. Employee employeeResult = null;
  100.  
  101. int count = 0;
  102.  
  103. /*
  104. * Descend the k-ary from the top down
  105. * by iterating the ceo(manager's) children
  106. * recursively until you get a non-null value
  107. */
  108. for (Employee reportingEmployee : ceo.getReports()) {
  109.  
  110. Employee e = closestCommonManager(reportingEmployee, firstEmployee, secondEmployee);
  111.  
  112. if (e != null) {
  113. count++;
  114. employeeResult = e;
  115. }
  116. }
  117.  
  118. // 2 non-null recursive results means a match, return the common ancestor
  119. if (count == 2) {
  120. return ceo;
  121. }
  122.  
  123. return employeeResult;
  124.  
  125. }
  126. };
Add Comment
Please, Sign In to add comment