Compute balances, rejections, and platform reserve
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given a set of transactions in CSV-like format with columns:
- `account_name`, `timestamp`, `currency`, `amount`
Assume:
- Transactions for all accounts can be interleaved; you must process them in **ascending timestamp order** (if input is already ordered, you may process in given order).
- All amounts are integers in the smallest currency unit (e.g., cents).
- Treat each `(account_name, currency)` as a separate balance unless stated otherwise.
## Part 1 — Non-zero ending balances
Compute each account’s ending balance (sum of its amounts). Output all accounts whose ending balance is non-zero, along with the balance.
## Part 2 — Reject transactions that would make balance negative
Process transactions in time order while maintaining balances.
- If applying a transaction would make the account’s balance negative, **reject** it (do not apply it).
- Collect rejected transactions as strings in the original row format (or a clear equivalent such as `"acct,timestamp,currency,amount"`).
Output:
1. The rejected transaction list.
2. All accounts with non-zero ending balances after applying only accepted transactions.
## Part 3(a) — Allow borrowing from a platform account
You are also given a `platform_id`.
Process transactions in time order with this additional rule:
- For any **non-platform** account, if after applying a transaction its balance would become negative, the account may “borrow” from the platform to bring it back up to 0 **as long as the platform has sufficient funds**.
- Borrow amount = `-(new_balance)`.
- Decrease the platform’s balance by the borrowed amount.
- The transaction is accepted if borrowing succeeds.
- If the platform does not have enough balance to cover the borrow, the transaction is rejected (and not applied).
Additionally compute:
- `max_reserve`: the **maximum total amount borrowed from the platform at any point in time** (i.e., peak borrowed amount outstanding). You should define and track this consistently based on your borrowing model.
Output for Part 3(a):
1. `max_reserve`
2. `rejected_transactions`
3. All accounts with non-zero ending balances after processing.
## Notes
- Clearly document how you compute `max_reserve` (e.g., peak cumulative borrowed amount across accounts over time).
- If multiple currencies exist, state whether borrowing is allowed only within the same currency (recommended) or across currencies (not recommended). Implement the chosen assumption consistently.
Quick Answer: This question evaluates transactional state management, time-ordered stream processing, balance reconciliation, handling of rejected transactions, and modeling inter-account borrowing with platform reserve accounting.