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

What this problem is really about

Anyone can build the per-project budget map and do the division. What separates candidates is the accumulation: an employee can sit on several projects, so each project they touch has to ADD to their running total, not replace it. Reach for a plain assignment instead of a running sum and an employee on two projects walks away with only the last project's share. The other quiet trap is the divisor: the headcount has to be that project's own team, counted before you divide, not the total number of unique people across the company, or every share comes out too small.


Break down the requirements

Step 1: Look up each budget and tally each project's team

In one pass over assignments, tally how many employees each project_id has (headcount) and collect that project's members into team_by_project. Build a budget_by_project lookup so you can fetch any project's budget by id.

Step 2: Split each project's budget among its own team

Walk projects, not the assignments, so unstaffed projects are visible. For each project the per-employee share is budget / headcount. Skip any project whose headcount is zero (if n == 0: continue) so an unstaffed project never triggers a divide-by-zero and its budget simply stays unallocated.

Step 3: Accumulate each employee's share across their projects

For every member of a project's team, add that project's share into a result dict keyed by employee_id. An employee on several projects accumulates the sum of every project's share, never just the last one.


The solution

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

Common follow-up questions