Implement an account balance tracker that parses deposit, withdraw and transfer transaction lines
Company: Bloomberg
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Build a simple in-memory ledger that tracks every user's cash balance while processing a stream of text transactions.
Supported transactions:
| Kind | Format | Effect |
|---|---|---|
| Deposit | `DEPOSIT <User> <Amount>` | Increase the user's balance by Amount |
| Withdraw | `WITHDRAW <User> <Amount>` | Decrease the user's balance by Amount |
| Transfer | `TRANSFER <FromUser> <ToUser> <Amount>` | Move Amount from FromUser to ToUser |
User names are case-insensitive, and balances are stored per unique name: `alice`, `ALICE` and `Alice` all refer to the same account.
Implement these methods:
- `AccountBalanceTracker()`: construct an empty tracker in which every balance is 0.
- `processTransactions(transactions: string[]) -> void`: apply a batch of transaction lines in order. Lines that are blank, malformed or would overdraw a balance are ignored.
- `getUserBalance(user: string) -> number`: return the user's current balance.
Example 1:
```text
tracker = AccountBalanceTracker()
tracker.processTransactions([
"DEPOSIT Alice 100",
"WITHDRAW Alice 30",
"DEPOSIT Bob 50",
"TRANSFER Alice Bob 50",
"WITHDRAW Bob 120",
])
tracker.getUserBalance("Alice") -> 20
tracker.getUserBalance("Bob") -> 100
```
Alice goes to 100, then 70. Bob gets 50. The transfer leaves Alice at 20 and Bob at 100. The last withdrawal is ignored because Bob has only 100, so his balance stays 100.
Example 2:
```text
tracker = AccountBalanceTracker()
tracker.processTransactions([
"DEPOSIT carol 40",
"",
"TRANSFER CAROL Dave 15",
"WITHDRAW Dave",
"TRANSFER Carol Dave 30",
"DEPOSIT Dave abc",
])
tracker.getUserBalance("Carol") -> 25
tracker.getUserBalance("DAVE") -> 15
```
`carol`, `CAROL` and `Carol` are one account. The blank line, the withdrawal with no amount and the deposit of `abc` are malformed and skipped. The second transfer is skipped because Carol has only 25 left.
```hint Order of work per line
Decide whether a line is valid (token count, keyword, a positive whole-number amount) before you touch any balance, and use the same normalized form of a name when you write a balance and when you read one.
```
### Constraints & Assumptions
- `1 <= number of transactions <= 10^4`
- `1 <= amount <= 10^4`
- At most `10^4` total calls to `processTransactions` and `getUserBalance`.
- All user names are non-empty alphanumeric strings.
### Clarifying Questions to Ask
- Are the keywords `DEPOSIT`, `WITHDRAW` and `TRANSFER` case-insensitive like user names, or must they be uppercase?
- Can fields be separated by more than one space or by tabs?
- Are amounts always whole numbers, or can they carry cents?
- Is a transfer from a user to themselves valid, and should it be applied or skipped?
- Is a withdrawal that brings a balance to exactly 0 allowed?
### What a Strong Answer Covers
- One normalization of user names, applied both when balances change and in `getUserBalance`.
- Validation of each line (token count, keyword, amount) before any balance changes, so a malformed line is skipped without raising an error or half-applying.
- Overdraft checks for withdrawals and for the sender of a transfer, with a transfer that changes both balances or neither.
- The balance returned for a user who has never appeared, and the time and space cost of each method.
### Follow-up Questions
- How would you report which lines were rejected and why, instead of dropping them silently?
- Amounts can now include cents, such as `12.50`. What type do you store balances in?
- How would you make a whole batch all-or-nothing, so that one bad line rejects the entire call?
- Several threads now call `processTransactions` at once. How do you keep transfers correct?
Overview: Build an in-memory ledger that applies DEPOSIT, WITHDRAW and TRANSFER text lines in order, ignores blank, malformed or overdrawing lines, and returns balances for case-insensitive user names. It tests input parsing, validating a line before changing state, and clean class design.