Problem Β· Hash Table
Account Balance Manager Part 2 - Reject Overdrafts
Learn this problemProblem statement
Practice sequence
- Part 1: Calculate Totals
- Part 2: Reject Overdrafts (you are here)
- Part 3: Platform Coverage
What the interview report shared
The coding round centered on Account Balance / Bank Transaction and contained three questions.
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.
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.
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".