Double Duty
A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Microsoft
Problem
A staffing system tracks which employees are assigned to each funded project, and every project splits its budget evenly across the people working on it. Given projects with their budgets and the assignments linking employees to projects, return each employee's total share across every project they're on; a project with nobody assigned leaves its budget unallocated.
Split the money. Some wore two hats.
Example
Input:
projects = [{"budget":9000,"project_id":"P1"},{"budget":6000,"project_id":"P2"}], assignments = [{"project_id":"P1","employee_id":"alice"},{"project_id":"P1","employee_id":"bob"},{"project_id":"P1","employee_id":"carol"},{"project_id":"P2","employee_id":"alice"},{"project_id":"P2","employee_id":"bob"}]Output:
{"alice":6000,"bob":6000,"carol":3000}Worked solution and explanation
No single row of assignments can tell you what it is worth. 'alice' on 'P1' is worth 9000 divided by however many rows share that project_id, so the headcount has to exist before any share does. That makes this an aggregate-then-distribute problem with 2 groupings on 2 different keys: by project to size the team, then by employee to total what each person collected.
Headcount before shares
Step 1: Index the budgets
budget_by_project maps each project_id to its budget, so the payout loop reads a budget with one lookup.
Step 2: Size and list each team in one pass
A single walk over assignments fills headcount and team_by_project together. headcount.get(pid, 0) + 1 and setdefault(pid, []) both cope with the first time a project appears, so there's no separate initialisation.
Step 3: Skip the unstaffed project
While looping over projects, a project absent from headcount reads as 0 through headcount.get(pid, 0) and hits continue. Its budget stays unallocated, as the statement asks, and the division never sees a 0.
Step 4: Divide once, add many times
share is computed once per project, then added to each teammate's running total with result.get(emp, 0.0) + share. The + is what lets 'alice' keep her 3000.0 from 'P1' when 'P2' pays her another 3000.0.
def allocate_budget(projects, assignments):
budget_by_project = {p['project_id']: p['budget'] for p in projects}
headcount = {}
team_by_project = {}
for a in assignments:
pid = a['project_id']
headcount[pid] = headcount.get(pid, 0) + 1
team_by_project.setdefault(pid, []).append(a['employee_id'])
result = {}
for p in projects:
pid = p['project_id']
n = headcount.get(pid, 0)
if n == 0:
continue
share = budget_by_project[pid] / n
for emp in team_by_project[pid]:
result[emp] = result.get(emp, 0.0) + share
return resultLosing the 'P1' share
The easy slip is writing the share into result with plain assignment. It looks right for anyone on a single project, which is why it survives a quick check against 'carol'. On the example it returns 3000.0 for 'alice', 'bob' and 'carol' alike: the 'P2' share replaces the 'P1' share for the 2 people who work both.
def allocate_budget(projects, assignments):
team_by_project = {}
for a in assignments:
team_by_project.setdefault(a['project_id'], []).append(a['employee_id'])
result = {}
for p in projects:
team = team_by_project.get(p['project_id'], [])
for emp in team:
result[emp] = p['budget'] / len(team) # replaces any earlier share
return resultReturns {'alice': 3000.0, 'bob': 3000.0, 'carol': 3000.0} on the example: 6000 of the 15000 budgeted is gone.
- `result[emp] = share`
- Keeps only the last project processed for each person. 'alice' and 'bob' report 3000.0, and the output sums to 9000 against 15000 of staffed budget.
- `result.get(emp, 0.0) + share`
- Adds each project's share to the running total. 'alice' and 'bob' reach 6000.0, 'carol' stays at 3000.0, and the output sums to the full 15000.
Common follow-up questions
- The same employee appears twice on one project in
assignments. What does your function do?It counts 2 heads for that person, so they collect 2 shares and every share on the project shrinks; if a pair should count once, dedupe on the
project_idandemployee_idpair with a set before counting. Assignment feeds that get retried or re-imported pick up repeats like this all the time. - An assignment references a
project_idthat is missing fromprojects. Skip it, flag it, or fail?As written it is skipped silently, because payouts loop over
projectsand that id is never visited. In a pipeline you would count those orphan rows and report them, since a missing project usually means the 2 feeds are out of sync. - These are budgets. What happens when 10000 is split 3 ways?
Float division leaves each share with a repeating fraction, so paid amounts can drift from the budget by fractions of a cent; work in integer cents with
divmodand hand out the remainder by a fixed rule. Finance reconciles allocated totals against budgets, and a 1-cent gap becomes a ticket. - What tests would you write for this?
A person on 2 projects, a project with nobody, empty inputs, and an invariant: the output sums to the total budget of the staffed projects. The invariant catches the overwrite bug without hand-computing every expected value.
assignmentshas 200 million rows and won't fit in memory. Now what?The share needs the final headcount, so make 2 passes: one that only counts heads per project, then a second that adds
budget / nto each employee as rows stream by. That dropsteam_by_projectand leaves memory at one number per project and one per employee.