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.

Group by `project_id`, then equal-split accumulated per employee
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 result

Losing 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.

Assigning the share instead of adding it
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 result

Returns {'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