You are building an account balance management system for a payment platform. The system processes financial transactions across multiple accounts and needs to handle various business rules around balance validation and fund coverage.
The problem is divided into three progressive parts:
Balance Aggregation: Process transactions and return final account balances
Transaction Validation: Reject transactions that would cause negative balances
Platform Account Coverage: Use a designated platform account to cover insufficient funds
There are multiple parts to this problem -- ask the interviewer how many parts there are to better manage your time
Start with the simplest approach and extend it as parts get more complex
Write your own test cases and ensure your code compiles and runs correctly
You will receive a list of transactions, where each transaction is a dictionary with an account_id string and an amount integer (positive for deposits, negative for withdrawals):
transactions = [ {"account_id": "account_A", "amount": 100}, {"account_id": "account_B", "amount": 50}, {"account_id": "account_A", "amount": -30}, {"account_id": "account_B", "amount": -80}, ]
Implement a function get_account_balances(transactions) that processes all transactions and returns the final balance for each account that has a balance.
` transactions = [ {"account_id": "account_A", "amount": 100}, {"account_id": "account_B", "amount": 50}, {"account_id": "account_A", "amount": -30}, {"account_id": "account_C", "amount": 200}, {"account_id": "account_B", "amount": -50}, ]
get_account_balances(transactions)
`
Process transactions in order
Aggregate amounts by account ID
Return only accounts with balance greater than 0
Return as a dictionary mapping account_id to balance
Extend your solution to validate each transaction before processing. A transaction should be rejected if it would cause the account balance to become negative. Return both the final balances and the list of rejected transactions.
Implement process_transactions(transactions) that returns a tuple of (balances, rejected_transactions).
` transactions = [ {"account_id": "account_A", "amount": 100}, {"account_id": "account_A", "amount": -150}, # Would make balance -50 {"account_id": "account_B", "amount": 50}, {"account_id": "account_A", "amount": -80}, # Valid: 100 - 80 = 20 {"account_id": "account_B", "amount": -100}, # Would make balance -50 ]
process_transactions(transactions)
`
Process transactions in order
Reject any transaction that would make the account balance negative
Deposits (positive amounts) are always accepted
Return rejected transactions in the order they were encountered
Include accounts with zero balance in the result
The platform can now provide financial assistance. When a transaction would cause a negative balance, instead of rejecting it, the system should transfer funds from a designated platform account to cover the shortfall.
Implement process_with_coverage(transactions, platform_account_id) that returns the total amount of funds transferred from the platform account to cover all shortfalls.
` transactions = [ {"account_id": "platform", "amount": 1000}, {"account_id": "account_A", "amount": 100}, {"account_id": "account_A", "amount": -150}, # Needs 50 coverage {"account_id": "account_B", "amount": 50}, {"account_id": "account_B", "amount": -100}, # Needs 50 coverage {"account_id": "account_A", "amount": -30}, # Needs 30 coverage ]
process_with_coverage(transactions, "platform")
`