Last active
February 9, 2022 14:23
-
-
Save h2rashee/8f8646df73e5505ee4f2e9b9bacad3e0 to your computer and use it in GitHub Desktop.
Rippling: Given an organization structure, find the employee with the highest average rating (including their subordinates)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A(3) | |
B(3) C(4) | |
D(1) E(2) | |
''' | |
max_avg_rating = 0 | |
max_avg_employee = None | |
class Employee: | |
def __init__(self, id, rating, subordinates=[]): | |
self.id = id | |
self.rating = rating | |
self.subordinates = subordinates | |
def get_best_performing_employee(self): | |
self._get_avg_rating() | |
return max_avg_employee | |
def get_avg_rating(self): | |
self._get_avg_rating() | |
return max_avg_rating | |
def _get_avg_rating(self): | |
global max_avg_rating | |
global max_avg_employee | |
if self.is_ic(): | |
if max_avg_rating < self.rating: | |
max_avg_rating = self.rating | |
max_avg_employee = self.id | |
return EmployeeRating(self.id, 1, self.rating) | |
subordinate_ratings = [subordinate._get_avg_rating() for subordinate in self.subordinates] | |
total_rating = sum(er.get_sum_perf_rating() for er in subordinate_ratings) | |
total_employees = sum(er.get_num_org_employees() for er in subordinate_ratings) | |
# Add the current manager's rating to his org's combined stats | |
total_rating = total_rating + self.rating | |
total_employees = total_employees + 1 | |
avg_rating = total_rating / total_employees | |
# Keep track of the current maximum average | |
if max_avg_rating is None or max_avg_rating < avg_rating: | |
max_avg_rating = avg_rating | |
max_avg_employee = self.id | |
return EmployeeRating(self.id, total_employees, total_rating) | |
# IC = Individual Contributor (no subordinates) | |
def is_ic(self): | |
return len(self.subordinates) == 0 | |
class EmployeeRating: | |
def __init__(self, id, num_org_employees, sum_perf_rating): | |
self.id = id | |
self.num_org_employees = num_org_employees | |
self.sum_perf_rating = sum_perf_rating | |
def get_num_org_employees(self): | |
return self.num_org_employees | |
def get_sum_perf_rating(self): | |
return self.sum_perf_rating | |
def get_avg_rating(self): | |
return self.get_sum_perf_rating() / self.get_num_org_employees() | |
# Basic testing infrastructure | |
def reset(): | |
''' | |
We need to wipe the global variable in between runs to avoid tainting the data | |
''' | |
global max_avg_rating | |
global max_avg_employee | |
max_avg_rating = 0 | |
max_avg_employee = None | |
# Sample test cases | |
org_single_person = Employee('A', 3) | |
org_vanilla_test_case = Employee('A', 3, [Employee('B', 3), Employee('C', 4, [Employee('D', 1), Employee('E', 2)])]) | |
org_ceo_best_test_case = Employee('A', 100, [Employee('B', 3), Employee('C', 4, [Employee('D', 1), Employee('E', 2)])]) | |
org_ic_best_test_case = Employee('A', 3, [Employee('B', 3), Employee('C', 4, [Employee('D', 10), Employee('E', 2)])]) | |
org_middle_manager_best_test_case = Employee('A', 3, [Employee('B', 3), Employee('C', 14, [Employee('D', 1), Employee('E', 2)])]) | |
assert org_single_person.get_best_performing_employee() == 'A' | |
reset() | |
assert org_vanilla_test_case.get_best_performing_employee() == 'B' | |
reset() | |
assert org_ceo_best_test_case.get_best_performing_employee() == 'A' | |
reset() | |
assert org_ic_best_test_case.get_best_performing_employee() == 'D' | |
reset() | |
assert org_middle_manager_best_test_case.get_best_performing_employee() == 'C' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment