Yearly Account Balance with Conditional Monthly Service Charges
Arrays & HashingAuto-graded · 18 test casesLast updated
Simulate a year of account activity, applying a flat monthly charge unless a month has enough payments with a large enough total, and return the final balance.
Yearly Account Balance with Conditional Monthly Service Charges
An account starts the year with a given balance. During the year a list of transactions is applied to it, and at the close of each of the twelve months a flat service charge of 5 may be deducted.
Each transaction is a dictionary with these keys:
amount: a number, the size of the transactionmonth: an integer from 1 to 12type: a string, either"payment"or"deposit"
Rules
- A
"payment"lowers the balance by itsamount. Any other transaction (a"deposit") raises the balance by itsamount. - Every month from 1 through 12 is evaluated, including months with no transactions at all.
- For a given month, the service charge is waived only if both of these hold, counting only transactions of type
"payment"in that month:- there are at least 3 payments, and
- the sum of those payment amounts is strictly greater than 100.
- If the charge is not waived, 5 is subtracted from the balance for that month.
- Deposits never contribute to the waiver check.
- The balance may become negative; do not clamp it.
- Return the balance after all transactions and all twelve monthly charge decisions have been applied.
Write a function that takes the starting balance and the list of transactions, and returns the final balance as a float.
Constraints
initial_balance >= 00 <= len(transactions) <= 1000- Each
monthis an integer in[1, 12] - Each
typeis either"payment"or"deposit" - The transactions may appear in any order
Examples
Example 1
Input: compute_year_end_balance(200, [{'amount': 50, 'month': 1, 'type': 'payment'}, {'amount': 30, 'month': 1, 'type': 'payment'}, {'amount': 40, 'month': 1, 'type': 'payment'}])
Output: 25
Example 2
Input: compute_year_end_balance(1000, [])
Output: 940
Example 3
Input: compute_year_end_balance(500, [{'amount': 20, 'month': 2, 'type': 'payment'}, {'amount': 30, 'month': 2, 'type': 'payment'}, {'amount': 40, 'month': 2, 'type': 'payment'}, {'amount': 200, 'month': 2, 'type': 'deposit'}])
Output: 550
Hints
Hint 1
Whether a month's charge is waived depends only on that month's payment count and payment total, never on the running balance.
Hint 2
Since everything is plain addition and subtraction, the order of operations does not affect the final balance. You can handle all transactions first, then all twelve charges.
Hint 3
Keep a payment count and a payment total for each month. Charge 5 when the count is below 3 or the total is not strictly above 100, and remember that empty months are charged too.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The waiver for a month depends only on two figures: how many payments were made in that month and what they add up to. The running balance plays no part. That lets us split the work into two independent pieces:
- Transaction effect. Go through the list once. Subtract each payment and add each deposit to the balance. While doing so, record for each month the number of payments and their total.
- Charge effect. For each month 1 to 12, subtract 5 unless the month had at least 3 payments and a payment total strictly above 100.
Why it is correct
- Every change to the balance is a plain addition or subtraction, with no clamping. Addition is commutative, so applying all transactions first and all charges afterwards gives the same result as a month-by-month simulation.
- The per-month arrays hold exactly the payments belonging to each month, which is the data the waiver rule refers to.
- The boundaries match the statement: the waiver needs
count >= 3andtotal > 100, so the charge applies whencount < 3ortotal <= 100. Three payments totaling exactly 100 are still charged. - Months with no payments have count 0, so they are charged. Deposits never touch the counters.
For example, with a start of 200 and three January payments of 50, 30 and 40 (total 120), January is waived and the balance is 80. The other eleven months are empty and each costs 5, giving 80 - 55 = 25.
Complexity
- Time: O(n), one pass over the transactions plus a fixed 12-step loop.
- Space: O(1), two arrays of size 13.
Reference solution
from typing import List, Dict
def compute_year_end_balance(starting_balance: float, transactions: List[Dict]) -> float:
balance = float(starting_balance)
payment_counts = [0] * 13
payment_totals = [0.0] * 13
for tx in transactions:
amount = float(tx["amount"])
month = tx["month"]
if tx["type"] == "payment":
balance -= amount
payment_counts[month] += 1
payment_totals[month] += amount
else:
balance += amount
for month in range(1, 13):
if payment_counts[month] < 3 or payment_totals[month] <= 100.0:
balance -= 5.0
return balance