A statement shows the balance after every transaction, not just the total.
Every input row, with the columns txn_id, account_id, posted_at, amount and balance — the sum of this account's amount up to and including this transaction, rounded to 2 decimal places.
Order by posted_at, breaking ties with the smaller txn_id first.
| txn_id | account_id | posted_at | amount | balance |
|---|---|---|---|---|
| 1 | A1 | 2024-01-01T00:00:00Z | 100.0 | 100.0 |
| 2 | A1 | 2024-01-02T00:00:00Z | 50.0 | 150.0 |
| 3 | A1 | 2024-01-03T00:00:00Z | -30.0 | 120.0 |
posted_at. They get different balances, in txn_id order — the earlier one does not yet include the later.RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, and RANGE advances a whole group of tied rows at once.amount adds nothing and does not blank the balance.transactions| amount | txn_id | posted_at | account_id |
|---|---|---|---|
| 100 | 1 | 2024-01-01T00:00:00Z | A1 |
| 50 | 2 | 2024-01-02T00:00:00Z | A1 |
| -30 | 3 | 2024-01-03T00:00:00Z | A1 |
| txn_id | account_id | posted_at | amount | balance |
|---|---|---|---|---|
| 1 | A1 | 2024-01-01T00:00:00Z | 100 | 100 |
| 2 | A1 | 2024-01-02T00:00:00Z | 50 | 150 |
| 3 | A1 | 2024-01-03T00:00:00Z | -30 | 120 |
transactions| amount | txn_id | posted_at | account_id |
|---|---|---|---|
| 10 | 1 | 2024-02-01T00:00:00Z | A1 |
| 99 | 2 | 2024-02-01T00:00:00Z | A2 |
| 5 | 3 | 2024-02-02T00:00:00Z | A1 |
| txn_id | account_id | posted_at | amount | balance |
|---|---|---|---|---|
| 1 | A1 | 2024-02-01T00:00:00Z | 10 | 10 |
| 2 | A2 | 2024-02-01T00:00:00Z | 99 | 99 |
| 3 | A1 | 2024-02-02T00:00:00Z | 5 | 15 |
Community-reported interview topic. Not an official company question and no affiliation is implied.
Run against the public tests, or submit to score against all of them.