# The Budget Allocator

> Split the money. Some wore two hats.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given projects (list of dicts with project_id and budget) and assignments (list of dicts with project_id and employee_id), compute each employee's share. Each project's budget is split equally among its assigned employees. Return a dict mapping employee_id to their total share across all projects they are on. If a project has no assignees, its budget is not distributed.

## Worked solution and explanation

### Why this problem exists in real interviews

Splitting budgets proportionally across employees tests **many-to-many relationship handling**: each project has a budget and multiple employees, and each employee may span multiple projects. It checks dict accumulation with division.

---

### Break down the requirements

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

Each employee on a project gets `budget / num_employees` of that project's budget.

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

Accumulate each employee's total budget share in a result dict.

---

### The solution

**Per-project equal split accumulated per employee**

```python
def allocate_budgets(projects):
    result = {}
    for project in projects:
        budget = project['budget']
        employees = project['employees']
        share = budget / len(employees)
        for emp in employees:
            if emp not in result:
                result[emp] = 0.0
            result[emp] += share
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(P * E) where P is the number of projects and E is the average number of employees per project.
> 
> **Space:** O(U) where U is the number of unique employees.

> **Interviewers Watch For**
>
> Correctly dividing the budget by the number of employees per project, not by the total number of unique employees across all projects.

> **Common Pitfall**
>
> Dividing by the total unique employee count instead of per-project employee count. 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/len(employees)`.)_
- 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 employees tables with budget/count window function.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_budget_allocator)
- [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). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.