Compute a 2020 bank account's year-end balance after all transactions and twelve monthly card-fee decisions, applying the waiver only when both payment-count and spending thresholds are met.
## Problem
You are given every transaction on one bank account during the calendar year 2020. The starting balance is zero. `amounts[i]` and `dates[i]` describe one transaction:
- A negative amount is a card payment.
- Zero or a positive amount is an incoming transfer.
- A date is formatted `YYYY-MM-DD` and is always in 2020.
The card has a fee of `5` charged at the end of each month. The fee is waived for a month only when that month contains at least three card payments and the total absolute value of those card payments is at least `100`.
Return the final balance after all transactions and all twelve monthly fee decisions.
### Function Contract
Implement `yearEndBalance(amounts, dates)` and return an integer.
### Constraints & Assumptions
- `0 <= len(amounts) = len(dates) <= 200,000`.
- `-10^9 <= amounts[i] <= 10^9`.
- Dates are valid 2020 dates and transactions may be supplied in any order.
- A zero amount is an incoming transfer, not a card payment.
- The total cost of card payments is the sum of `-amount` for negative transactions only.
- Every month with no qualifying card activity pays the fee, including months with no transactions.
- The final result fits in a signed 64-bit integer.
### Clarifying Questions to Ask
- Is the waiver threshold inclusive? Yes: at least three payments totaling at least `100`.
- Are refunds represented by positive amounts and excluded from card-payment totals? Yes.
- Does transaction order within a month matter? No.
- Are there always twelve fee decisions? Yes.
```hint Aggregate only what the waiver needs
For each month, retain a card-payment count and the sum of absolute negative amounts; the account balance itself is simply the sum of all transactions.
```
### Examples
- `amounts = [100, 100, 100, -10]` and December dates returns `230`: transaction sum `290` minus twelve fees.
- `amounts = [180, -50, -25, -25]` with all transactions in January returns `25`: January qualifies, so only eleven fees are charged.
- `amounts = [1, -1, 0, -105, 1]` with two April card payments returns `-164`: April reaches total cost `106` but has only two payments, so it does not qualify.
### Evaluation Focus
- Counts only negative transactions as card payments and includes all amounts in the balance.
- Evaluates count and cost thresholds per calendar month.
- Charges months with no transactions.
- Runs in `O(n)` time with `O(12)` auxiliary state.
### Extensions to Discuss
1. How would varying monthly fees and waiver policies be represented safely?
2. What changes for a streaming ledger that can receive corrections?
3. How would multi-currency transactions affect the monthly threshold calculation?
Quick Answer: Compute a 2020 bank account's year-end balance after all transactions and twelve monthly card-fee decisions, applying the waiver only when both payment-count and spending thresholds are met.
You are given every transaction on one bank account during the calendar year 2020. The starting balance is zero. amounts[i] and dates[i] describe one transaction:
A negative amount is a card payment.
Zero or a positive amount is an incoming transfer.
A date is formatted
YYYY-MM-DD
and is always in 2020.
The card has a fee of 5 charged at the end of each month. The fee is waived for a month only when that month contains at least three card payments and the total absolute value of those card payments is at least 100.
Return the final balance after all transactions and all twelve monthly fee decisions.
Function Contract
Implement yearEndBalance(amounts, dates) and return an integer.
Constraints & Assumptions
0 <= len(amounts) = len(dates) <= 200,000
.
-10^9 <= amounts[i] <= 10^9
.
Dates are valid 2020 dates and transactions may be supplied in any order.
A zero amount is an incoming transfer, not a card payment.
The total cost of card payments is the sum of
-amount
for negative transactions only.
Every month with no qualifying card activity pays the fee, including months with no transactions.
The final result fits in a signed 64-bit integer.
Clarifying Questions to Ask Guidance
Is the waiver threshold inclusive? Yes: at least three payments totaling at least
100
.
Are refunds represented by positive amounts and excluded from card-payment totals? Yes.
Does transaction order within a month matter? No.
Are there always twelve fee decisions? Yes.
Examples
amounts = [100, 100, 100, -10]
and December dates returns
230
: transaction sum
290
minus twelve fees.
amounts = [180, -50, -25, -25]
with all transactions in January returns
25
: January qualifies, so only eleven fees are charged.
amounts = [1, -1, 0, -105, 1]
with two April card payments returns
-164
: April reaches total cost
106
but has only two payments, so it does not qualify.
Evaluation Focus
Counts only negative transactions as card payments and includes all amounts in the balance.
Evaluates count and cost thresholds per calendar month.
Charges months with no transactions.
Runs in
O(n)
time with
O(12)
auxiliary state.
Extensions to Discuss
How would varying monthly fees and waiver policies be represented safely?
What changes for a streaming ledger that can receive corrections?
How would multi-currency transactions affect the monthly threshold calculation?