Net revenue per user after refunds
Problem
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
def revenue_per_user(orders: list[tuple[int, str, float]], refunded_ids: list[int]) -> dict[str, float]: ...Examples
The visible tests, as an interactive Python session would show them: each call and the value it should give.
>>> 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})
TrueHints
Think about which refunds matter: a refund set gives O(1) lookup.
Accumulate per-user totals only for orders whose id is not in the refunded set.
Seed each user with 0.0 before adding — fully-refunded users must still appear.
Reference solution
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 outWhat 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.