Editor

Bank Statement With Overdraft Fees

#298 · Working with data · Intermediate · 70 XP · about 14 min

A bank statement starts from an opening balance and lists the transactions in date order as (date, description, amount) tuples. Dates are strings 'YYYY-MM-DD'. Amounts are whole numbers of cents: positive for money in, negative for money out (a payment), and never 0.

The bank charges an overdraft fee of fee cents whenever a payment leaves the balance below zero, but never more than one fee on the same date. The fee is a line of its own, straight after the payment that caused it, with the description 'Overdraft fee' and the amount -fee. Money coming in never causes a fee, and neither does the fee itself.

Write statement(opening, transactions, fee) that returns the full statement as a list of (date, description, amount, balance) tuples, where balance is the balance after that line.

Examples

statement(5000, [
    ('2026-03-01', 'Coffee', -450),
    ('2026-03-02', 'Rent', -6000),
    ('2026-03-02', 'Bus', -300),
    ('2026-03-03', 'Salary', 10000),
    ('2026-03-04', 'Shoes', -9000),
], 2500)
# returns [('2026-03-01', 'Coffee', -450, 4550),
#          ('2026-03-02', 'Rent', -6000, -1450),
#          ('2026-03-02', 'Overdraft fee', -2500, -3950),
#          ('2026-03-02', 'Bus', -300, -4250),
#          ('2026-03-03', 'Salary', 10000, 5750),
#          ('2026-03-04', 'Shoes', -9000, -3250),
#          ('2026-03-04', 'Overdraft fee', -2500, -5750)]

The bus fare on 2 March also leaves the balance below zero, but that day has already had its fee.

Constraints

  • 0 <= len(transactions) <= 10000
  • 1 <= fee <= 10000, and the opening balance can be negative.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.