You are given a list of financial transactions in CSV-like rows:
account_name, timestamp, currency, amount
account_name
is a string ID.
timestamp
is an integer (not guaranteed to be sorted).
currency
is a string like
"usd"
.
amount
is an integer (positive = credit, negative = debit).
Assume all arithmetic is integer. Unless otherwise stated, process transactions in increasing timestamp order (stable tie-breaker by input order).
Compute each account’s final balance (you may assume all rows are the same currency, or treat balances independently per (account_name, currency)—state your assumption). Output all accounts with non-zero final balance.
Example Input:
account_name, timestamp, currency, amount
acct_123,1,usd,1000
acct_123,2,usd,500
acct_321,3,usd,400
acct_321,4,usd,-400
Output:
acct_123
has balance
1500
acct_321
is omitted (balance
0
)
Process transactions in time order while maintaining balances.
A transaction is rejected if applying it would make the account’s balance negative (i.e., balance + amount < 0). Rejected transactions:
rejected_transactions
in the order encountered,
At the end, output:
rejected_transactions
(full original rows for rejected ones), and
Example Input:
account_name, timestamp, currency, amount
acct_123,1,usd,1000
acct_123,2,usd,500
acct_321,3,usd,400
acct_321,4,usd,-500
Output:
acct_123 = 1500
,
acct_321 = 400
rejected_transactions = ["acct_321,4,usd,-500"]
You are also given a special platform_id account.
Process transactions in time order with the following rule:
0
.
Output three things:
max_reserve
: the
maximum total outstanding amount borrowed from the platform at any time
(peak platform exposure),
rejected_transactions
,
Example (platform_id = acct_123)
Input:
account_name, timestamp, currency, amount
acct_123,1,usd,1000
acct_321,3,usd,400
acct_321,4,usd,-500
Explanation: acct_321 would go to -100, so it borrows 100 from the platform to return to 0.
Output:
max_reserve = 100
rejected_transactions = []
acct_123 = 900
(and
acct_321
omitted if it ends at
0
)
O(n log n)
due to sorting, or
O(n)
if already sorted).