# The Generous Ones

> The generous ones are obvious.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given two parallel lists user_ids and tips, sum each user's tips and return the user_id with the highest total. If tied, return the alphabetically first user_id.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **parallel list aggregation**, where two lists share indices as an implicit join key. It probes dict-based grouping from parallel arrays and max-by-value selection.

---

### Break down the requirements

#### Step 1: Aggregate tips by user

Iterate both lists by index, accumulating tip amounts per user_id.

#### Step 2: Find the user with the highest total

Scan the totals dict for the maximum value and return the corresponding key.

---

### The solution

**Parallel list aggregation with max selection**

```python
def top_tipper(user_ids: list, tips: list) -> str:
    totals = {}
    for i in range(len(user_ids)):
        uid = user_ids[i]
        if uid in totals:
            totals[uid] += tips[i]
        else:
            totals[uid] = tips[i]
    best_user = None
    best_total = -1
    for uid, total in totals.items():
        if total > best_total:
            best_total = total
            best_user = uid
    return best_user
```

> **Time and Space Complexity**
>
> **Time:** O(n) for aggregation plus O(k) for finding the max, where k is the number of unique users.
> 
> **Space:** O(k) for the totals dict.

> **Interviewers Watch For**
>
> Whether you use `zip(user_ids, tips)` or manual indexing. Both work, but `zip` is more Pythonic.

> **Common Pitfall**
>
> Returning the highest individual tip instead of the highest total. The problem asks for the user with the highest sum across all their tips.

---

## Common follow-up questions

- What if ties should return all tied users? _(Tests collecting all users sharing the max total.)_
- How would you handle this with a single dict of lists? _(Tests using `defaultdict(list)` and summing after grouping.)_
- What if user_ids and tips have different lengths? _(Tests defensive coding: iterate up to the shorter length or raise an error.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_generous_ones)
- [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.