Account Balance Manager Part 2 - Reject Overdrafts
Problem statement
Practice sequence
- Part 1: Calculate Totals
- Part 2: Reject Overdrafts (you are here)
- Part 3: Platform Coverage
Reported transaction format
The linked interview report uses account_name,timestamp,currency,amount rows. Its examples are chronological and use one currency. Positive amounts add money and negative amounts withdraw money.
Practice contract
For this exercise, assume transactions arrive in order as account_id,amount strings and every account starts with a balance of 0.
- Reject a transaction when applying it would make that account's balance negative, and leave the balance unchanged.
- Otherwise, accept the transaction and update the balance. A resulting balance of exactly
0is allowed.
Return the rejected transaction strings in their original order.
The runner projects each chronological, single-currency source row to account_id,amount. Timestamps and currency are omitted; do not infer sorting or currency-conversion behavior. The existing callable return format defines the judged task.
Interview follow-up: Include final balances
The report also asks for the final nonzero balances after rejecting overdrafts. Keep the accepted-state balance map and, after all rows, list its nonzero entries. This follow-up is discussed alongside the runner’s rejection-only result.
account_name,timestamp,currency,amount
acct_123,1,usd,1000
acct_123,2,usd,500
acct_321,3,usd,400
acct_321,4,usd,-500The rejected original row is acct_321,4,usd,-500. The accepted final balances are acct_123:1500 and acct_321:400. The original report’s inconsistent account labels are resolved from the input identifiers. The judged projection returns ["acct_321,-500"].
Complete report exercise
As an additional practice exercise, implement fullOverdraftReport(String[] transactions). Each input string has exactly four comma-separated fields account_name,timestamp,currency,amount, with no header. Rows are already chronological, all use one currency, and the account and numeric bounds above apply. Do not sort transactions or convert currency.
Return a String[][] with exactly two rows: row 0 contains the rejected original four-field strings in input order; row 1 contains final nonzero account:balance strings in ASCII lexicographic account order. Preserve each rejected row exactly. This serialization is an authored practice choice. The workspace judges only rejectedTransactions on its two-field input; it does not grade this additional method.
For the four source rows above, the full report is [["acct_321,4,usd,-500"],["acct_123:1500","acct_321:400"]]. The last withdrawal would produce 400 + (-500) = -100, so reject it and leave that account at 400. This resolves the report’s conflicting minus sign and account-label typos from its signed inputs.
Function
rejectedTransactions(transactions: String[]) → String[]Examples
Example 1
transactions = ["account_A,100", "account_A,-150", "account_B,50", "account_A,-80", "account_B,-100"]return = ["account_A,-150", "account_B,-100"]Step 1: account_A +100 -> 100 (ok). Step 2: account_A -150 -> would be -50 (REJECT). Step 3: account_B +50 -> 50 (ok). Step 4: account_A -80 -> 100-80=20 (ok). Step 5: account_B -100 -> would be -50 (REJECT). The two rejected transactions are returned in order.
Example 2
transactions = ["account_A,40", "account_A,-40", "account_A,-1"]return = ["account_A,-1"]account_A +40 -> 40 (ok). account_A -40 -> exactly 0, which is allowed (ok). account_A -1 -> would be -1 (REJECT). Only the last transaction is rejected.
Example 3
transactions = ["acct_123,1000","acct_123,500","acct_321,400","acct_321,-500"]return = ["acct_321,-500"]The final withdrawal would make acct_321 negative, so it is rejected and that account remains400. The runner returns the projected two-field rejected row; the reported final-balance follow-up also yields acct_123:1500 and acct_321:400.
Constraints
- Each transaction is
"account_id,amount"with an integer amount. - Process in order; reject a transaction whose application would make the balance strictly negative.
- Deposits are always allowed; a result of exactly 0 is allowed.
- A rejected transaction does not change any balance.
- Return rejected transactions in order as
"account_id,amount". - For this practice domain, parsed amounts, all intermediate additions, balances, and negated shortfalls fit in a signed 64-bit integer. Account identifiers are nonempty ASCII strings without commas or colons.