All problems

Checking vs. Savings: Where's the Money Moving?

mediumPythonMergeGroupBy

Northline's product team is deciding whether to promote savings accounts harder, and wants aggregate cash-flow by account type first. They never look at the whole ledger at once: every promotion has a window, and the question is always about the money that has moved since that window opened.

Customers

id name account_type city
1 Farah Idris checking Chicago
2 Grant Boyle savings Miami
3 Hana Suzuki checking Chicago
4 Ibrahim Njoku savings Miami
5 Jade Wu checking Seattle

Transactions

id customer_id amount type txn_date
1 1 500 deposit 2023-07-01
2 1 -120 withdrawal 2023-07-03
3 2 1000 deposit 2023-07-02
4 2 -300 withdrawal 2023-07-05
5 3 250 deposit 2023-07-04
6 3 -600 withdrawal 2023-07-06
7 4 800 deposit 2023-07-07
8 4 -450 withdrawal 2023-07-08
9 5 2000 deposit 2023-07-09
10 5 -1500 withdrawal 2023-07-10
11 1 -200 transfer 2023-07-11
12 3 400 deposit 2023-07-12

Input

Both tables above are already built for you — those rows are not read from input. What does arrive is a single line holding the first day of the window, written the same way txn_date writes its dates.

Task: Counting only transactions dated on or after that day, print a dict mapping each account_type → the total net amount moved by the customers holding that kind of account. An account type with nothing in the window does not appear at all, and a window with no transactions in it gives an empty dict rather than an error.

Example: if only savings accounts had moved in some window and they had netted two hundred and fifty dollars, you'd print {'savings': 250}.

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…