---
title: Net revenue per user after refunds
url: https://gurkanozkan.ai/learn/coding/mle-net-revenue-per-user-after-refunds-01/
markdown: https://gurkanozkan.ai/learn/coding/mle-net-revenue-per-user-after-refunds-01.md
kind: coding
difficulty: Warm-up
topics:
  - Data manipulation
  - Hash map
  - Join
json: https://gurkanozkan.ai/api/learn/coding/mle-net-revenue-per-user-after-refunds-01.json
robots: noindex
---

# Net revenue per user after refunds

You get a list of orders `(order_id, user_id, amount)` and a list of refunded order IDs. Return a dict mapping every user who appears in `orders` to the total amount of their non-refunded orders (`0.0` if all were refunded). Ignore refund IDs that match no order.

## Signature

```python
def revenue_per_user(orders: list[tuple[int, str, float]], refunded_ids: list[int]) -> dict[str, float]: ...
```

## Examples

Each call and the value it should give.

```python
>>> close(revenue_per_user([(1,'u1',10.0),(2,'u1',5.5),(3,'u2',7.0)], []), {'u1': 15.5, 'u2': 7.0})
True

>>> close(revenue_per_user([(1,'u1',10.0),(2,'u1',5.5),(3,'u2',7.0)], [2]), {'u1': 10.0, 'u2': 7.0})
True
```

## Hints

1. Think about which refunds matter: a refund set gives O(1) lookup.
2. Accumulate per-user totals only for orders whose id is not in the refunded set.
3. Seed each user with 0.0 before adding — fully-refunded users must still appear.

## Reference solution

```python
def revenue_per_user(orders, refunded_ids):
    refunded = set(refunded_ids)
    out = {}
    for oid, user, amount in orders:
        out.setdefault(user, 0.0)
        if oid not in refunded:
            out[user] += amount
    return out
```

## What a strong solution covers

- Converts refunds to a set for O(1) lookups (overall O(n + r)).
- Keeps users whose orders were all refunded, at 0.0.
- Discusses the SQL equivalent (LEFT JOIN plus a filter, or SUM with a CASE expression).
- Mentions float vs Decimal for money.

## Common mistakes

- dropping fully refunded users
- quadratic membership checks on a list.
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
