Daily Balances And Volumes

Start Timer

0:00:00

Upvote
1
Downvote
Save question
View comments

Let’s say we work at Coinbase and have a log of user transactions, where each one changes the user’s BTC balance and is paid for by ACH or credit card. Write a function daily_summary that returns, for each user and each day they had activity, their BTC balance at the end of that day and how much they paid by each method that day.

Each event in events is a dict with these fields:

Field Meaning
user_id The user
ts ISO timestamp string
btc Signed change to the user’s BTC balance
payment_type 'ach' or 'credit_card'
amount Payment amount, an integer

Return one dict per user and day with these keys:

Key Meaning
user_id The user
day The calendar date written in ts (its first 10 characters, ignoring any UTC offset)
end_of_day_btc_balance Running BTC balance after the user’s last event that day
total_ach_volume Sum of ACH amounts that day
total_credit_card_volume Sum of credit card amounts that day

Note: The balance carries over from day to day, so it includes every event on that day and on all earlier days.

Rules - Only return days on which the user has at least one event. - Sort by user, then day, both ascending. - Round the balance to 8 decimal places. Volumes are plain sums with no rounding.

Example:

Input:

events = [
    {'user_id': 1, 'ts': '2026-04-03 09:00', 'btc': 0.5, 'payment_type': 'ach', 'amount': 100},
    {'user_id': 1, 'ts': '2026-04-03 15:00', 'btc': 0.2, 'payment_type': 'credit_card', 'amount': 40},
    {'user_id': 1, 'ts': '2026-04-04 10:00', 'btc': -0.1, 'payment_type': 'ach', 'amount': 20},
]

Output:

daily_summary(events) -> [
    {'user_id': 1, 'day': '2026-04-03', 'end_of_day_btc_balance': 0.7, 'total_ach_volume': 100, 'total_credit_card_volume': 40},
    {'user_id': 1, 'day': '2026-04-04', 'end_of_day_btc_balance': 0.6, 'total_ach_volume': 20, 'total_credit_card_volume': 0},
]
.
.
.
.
.


Comments

Loading comments