# Double Duty

> Split the money. Some wore two hats.

Canonical URL: <https://datadriven.io/problems/double_duty>

Domain: Python · Difficulty: medium · Seniority: L4

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

## 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: Count the headcount per project

Walk `assignments` once and count how many employees are assigned to each `project_id`. This headcount is the divisor for that project's budget.

#### Step 2: For each project, divide the budget equally among its employees

For each project, the per-employee share is `budget / headcount`. Skip any project with a headcount of zero so you never divide by zero.

#### Step 3: Sum each employee's share across all their projects

Walk `assignments` again and add each employee's per-project share into a result dict keyed by `employee_id`. An employee on multiple projects accumulates the sum of their shares.

---

### The solution

**Join on `project_id`, equal split accumulated per employee**

```python
def allocate_budget(projects, assignments):
    budget_by_project = {p['project_id']: p['budget'] for p in projects}

    headcount = {}
    for a in assignments:
        pid = a['project_id']
        headcount[pid] = headcount.get(pid, 0) + 1

    result = {}
    for a in assignments:
        pid = a['project_id']
        emp = a['employee_id']
        n = headcount.get(pid, 0)
        if n == 0:
            continue
        share = budget_by_project.get(pid, 0) / n
        result[emp] = result.get(emp, 0.0) + share
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(P + A) where P is the number of projects and A is the number of assignments (two linear passes over assignments plus one over projects).
> 
> Space: O(P + U) for the per-project budget/headcount maps and the per-employee result, where U is the number of unique employees.

> **Interviewers Watch For**
>
> Correctly dividing the budget by the number of employees on that project, not by the total number of unique employees across all projects, and guarding against a project with zero assignees.

> **Common Pitfall**
>
> Dividing by the total unique employee count instead of per-project headcount, or crashing on a project with no assignees. Each project's budget is split only among its own team.

---

## Common follow-up questions

- What if employees have weighted allocations instead of equal splits? _(Tests using individual weight fractions instead of `1/headcount`.)_
- What if the budget must be in whole cents with no remainder? _(Tests rounding strategies and distributing the leftover cents.)_
- How would you handle this in SQL? _(Tests JOIN between projects and assignments tables with budget/count window function.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/double_duty)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.