Katkat

Compute a Year-End Balance with Monthly Card Fees

DRWData Engineer · Online Assessment

Last updated

This problem tests logic implementation and data structure traversal, requiring the integration of conditional logic (fee waivers) into a sequential processing pipeline.

Problem Statement

You are given a starting bank balance and a history of transactions for the year 2020. Additionally, the bank charges a monthly card fee of $5 at the end of every month. The fee is waived for a given month if, within that month, the account processed at least 3 payments and the total spending across those payments exceeded $100.

Implement a function calculate_final_balance(initial_balance, transactions) where:

  • initial_balance is a float.
  • transactions is a list of dictionaries, each containing: amount (float), month (int, 1-12), and type (string, either "payment" or "deposit").

Rules

  1. Payments decrease the balance; deposits increase it.
  2. A "payment" is defined by the type field.
  3. At the end of each month (1-12), calculate if the fee waiver applies based on the payments made during that specific month.
  4. If the waiver does NOT apply, subtract $5 from the balance at the end of the month.
  5. Return the balance after all transactions and monthly fees for December have been processed.

Example

# Input
initial_balance = 200.0
transactions = [
    {"amount": 50.0, "month": 1, "type": "payment"},
    {"amount": 30.0, "month": 1, "type": "payment"},
    {"amount": 40.0, "month": 1, "type": "payment"}
]
# Output: 195.0 
# Explanation: 3 payments in Jan total $120. Waiver applies (>=3 payments, >$100). 
# Balance = 200 - 50 - 30 - 40 = 80. Then fee is skipped. Wait, the example logic: 
# 200 - 120 = 80. No fee applied. Final balance 80.0.

Constraints

  • initial_balance >= 0
  • transactions list length is between 0 and 1000
  • month is always between 1 and 12

Edge Cases

  • Months with zero transactions.
  • Months with payments but total spending under $100.
  • Account balance dipping below zero (allowed).

Hints

Hint 1

Group the transactions by month first using a dictionary or a list of lists of size 13.

Hint 2

For each month, calculate the sum of payments and count the number of payments separately.

Hint 3

Iterate through months 1 to 12 sequentially, updating the balance by applying all transactions for that month, then applying the fee if the waiver conditions are not met.

Follow-up questions

  • How would you modify your solution if the fee was variable based on the account's total assets?
  • If the transaction volume grew to millions of records, how would you optimize the computation using a distributed framework?
  • What changes are needed if the waiver logic allowed for 'rolling' totals across two consecutive months?