I had a phone screen interview with Noom recently. Here's the problem and solution:
A couch could have three types of responsibilities: personal coaching, group coaching and managing coaching.
They could have two types of allocations: baseline and temporary
Task: Given all data for a single coach and a date interval, calculate the percent of time spent working on each responsibility type. If the data isn't provided for a coach on a specific date, assume that they are performing baseline responsibilities.
Example:
Baseline- Personal Coaching: 50%, Group: 20%, Managing: 30%
Temporary: 2019-03-15- Managing: 100%
Temporary: 2019-03-16- Personal: 50%, Group: 50%, Managing: 50% (Reject this allocation since it doesn't add up to 100%. Hence assume the Baseline Allocation for this date)
The report for 2019-03-14 - 2019-03-15: Personal coaching: 25%, Group coaching: 10%, Managing: 65%
The report for 2019-03-14 - 2019-03-16: Personal coaching: 33.33%, Group coaching: 13.33%, Managing: 53.33%
Here's what I implemented:
from datetime import datetime
from collections import defaultdict
class Allocation:
def __init__(self, allocation_type, personal=0, group=0, managing=0):
self.allocation_type = allocation_type
self.personal = personal
self.group = group
self.managing = managing
class Responsibilities:
def __init__(self):
self.store_coach_data = defaultdict(dict)
self.baseline_data = defaultdict(Allocation)
def store_data(self, coach, allocation_type, date, personal=0, group=0, managing=0):
if allocation_type == 'baseline':
self.baseline_data[coach] = Allocation(allocation_type, personal, group, managing)
else:
if personal + group + managing == 100:
allocation = Allocation(allocation_type, personal, group, managing)
self.store_coach_data[coach][date] = allocation
else:
print 'Error: total is not 100, hence using baseline allocation.'
def generate_report(self, coach, date1, date2):
total_days = date2.day - date1.day + 1
if coach in self.baseline_data:
baseline = self.baseline_data[coach]
total_personal = 0
total_managing = 0
total_group = 0
coach_data = self.store_coach_data[coach] if coach in self.store_coach_data else None
if not coach_data:
print 'Personal Coaching: {}, Managing Coaching: {}, Group Coaching: {}'.format(baseline.personal, baseline.managing, baseline.group)
else:
count = 0
for date, allocation in coach_data.items():
if date1 <= date <= date2:
total_personal += allocation.personal
total_managing += allocation.managing
total_group += allocation.group
count += 1
total_personal += baseline.personal * (total_days - count)
total_personal = total_personal * 1.0 / total_days
total_managing += baseline.managing * (total_days - count)
total_managing = total_managing * 1.0 / total_days
total_group += baseline.group * (total_days - count)
total_group = total_group * 1.0 / total_days
print 'Personal Coaching: {}, Group: {}, Managing: {}'.format(total_personal, total_group, total_managing)
if __name__ == '__main__':
r = Responsibilities()
r.store_data('A', 'baseline', datetime(year=2019, day=14, month=3), 50, 0, 50)
r.store_data('A', 'temporary', datetime(year=2019, day=15, month=3), 0, 0, 100)
r.generate_report('A', datetime(year=2019, day=14, month=3), datetime(year=2019, day=15, month=3))
r.store_data('B', 'baseline', datetime(year=2019, day=14, month=3), 50, 0, 50)
r.store_data('B', 'temporary', datetime(year=2019, day=15, month=3), 0, 25, 75)
r.store_data('B', 'temporary', datetime(year=2019, day=16, month=3), 25, 25, 50)
r.store_data('B', 'temporary', datetime(year=2019, day=17, month=3), 25, 70, 5)
r.store_data('B', 'temporary', datetime(year=2019, day=18, month=3), 0, 25, 75)
r.store_data('B', 'temporary', datetime(year=2019, day=19, month=3), 0, 25, 55) # invalid
r.store_data('B', 'temporary', datetime(year=2019, day=20, month=3), 0, 5, 75) # invalid
r.generate_report('B', datetime(year=2019, day=14, month=3), datetime(year=2019, day=15, month=3))
r.generate_report('B', datetime(year=2019, day=14, month=3), datetime(year=2019, day=18, month=3))
r.generate_report('B', datetime(year=2019, day=14, month=3), datetime(year=2019, day=20, month=3))I was able to finish the exercise well within the allocated duration and the interview seemed happy. 1 week later, I got this feedback:
[[MY NAME]] managed to finish the exercise on time, but the code is of low quality and without care for any edge case or validations.Not sure what they were exactly looking for... Please let me know if I could have improved on something else perhaps..