
Running account balance
mediumRunning account balance
Stripe Pandas Interview Question
Stripe shows each connected account how its balance changes with every charge, refund and payout. Refunds and payouts are stored as negative amounts.
Using the transactions DataFrame, show every transaction's txn_id, account_id and the account's balance right after it as balance, rounded to 2 decimal places. A balance adds up all of that account's transactions up to and including this one, in date order. Sort the rows by account_id, then by date, earliest first. Assign the answer to result.
Asked of
- Data Analyst
- Analytics Engineer
- Data Engineer
- Data Scientist
- ML Engineer
transactionsDataFrame12 rows
| Column Name | Type |
|---|---|
| txn_id | int64 |
| account_id | int64 |
| created_date | str |
| type | str |
| amount | float64 |
transactionsExample Input
| txn_id | account_id | created_date | type | amount |
|---|---|---|---|---|
| 6 | 9001 | 2024-08-05 | payout | -150 |
| 2 | 9002 | 2024-08-01 | charge | 75.5 |
| 11 | 9001 | 2024-08-09 | charge | 99.99 |
| 7 | 9003 | 2024-08-05 | charge | 40 |
| 1 | 9001 | 2024-08-01 | charge | 120 |
| 10 | 9002 | 2024-08-08 | payout | -180 |
| 3 | 9001 | 2024-08-02 | charge | 60.25 |
| 9 | 9003 | 2024-08-06 | charge | 55.75 |
Example Output
| txn_id | account_id | balance |
|---|---|---|
| 1 | 9001 | 120 |
| 3 | 9001 | 180.25 |
| 6 | 9001 | 30.25 |
| 11 | 9001 | 130.24 |
| 2 | 9002 | 75.5 |
| 10 | 9002 | -104.5 |
| 7 | 9003 | 40 |
| 9 | 9003 | 95.75 |
Explanation
Account 9001's transactions are not stored in date order. In date order, a charge of 120.00 comes first and then one of 60.25, so the balance reaches 180.25. The payout of 150.00 on August 5 brings it down to 30.25.
The example above is a small slice of the data. Your code runs against the full DataFrames.
Company
Stripe
Difficulty
medium
Topic
running totals
Language
Pandas
Your code
Loading Python in the background. You can start writing.