Implement a card transaction authorizer with balance, block and high-frequency fraud checks
Company: Brex
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Implement an in-memory `CardTransactionAuthorizer` that approves or declines card purchases for cardholder accounts in real time. Each account has an available balance, an active or blocked status, and a short history of recent approved transactions that a basic fraud rule uses.
When a purchase arrives, the authorizer either approves it, debiting the amount from the available balance, or declines it with one of these reasons: `ACCOUNT_NOT_FOUND`, `ACCOUNT_BLOCKED`, `INSUFFICIENT_FUNDS`, `HIGH_FREQUENCY`. The fraud rule returns `HIGH_FREQUENCY` when there would be more than 3 approved transactions for the same account within a 120-second window, counting the current one. An earlier approval at time `s` is inside the window of a purchase at time `t` when `t - 120 <= s <= t`. When several reasons apply, return the first one in the order listed above. A declined purchase changes nothing: the balance stays the same and the purchase is not added to the history.
All amounts are non-negative integers in cents. Timestamps are non-negative integers in seconds and never decrease across calls to `authorize`.
Implement these methods:
- `createAccount(accountId: str, initialBalance: int) -> str` creates a new active account with the given starting balance. It returns `"CREATED"`, or `"ALREADY_EXISTS"` if the account already exists.
- `setBlocked(accountId: str, blocked: bool) -> str` blocks or unblocks an existing account. It returns `"OK"`, or `"ACCOUNT_NOT_FOUND"` if there is no such account.
- `authorize(accountId: str, amount: int, timestamp: int) -> str` returns `"APPROVED"` and debits the amount, or returns one of the four decline reasons.
- `getBalance(accountId: str) -> int` returns the current available balance, or -1 if the account does not exist.
Example 1:
```text
Input:
["CardTransactionAuthorizer", "createAccount", "authorize", "authorize", "authorize", "getBalance"]
[[], ["A1", 100], ["A1", 30, 10], ["A1", 80, 20], ["A1", 50, 30], ["A1"]]
Output: [null, "CREATED", "APPROVED", "INSUFFICIENT_FUNDS", "APPROVED", 20]
```
The first purchase leaves a balance of 70. The purchase of 80 is declined because 80 > 70, so the balance stays at 70, and the purchase of 50 then leaves 20.
Example 2:
```text
Input:
["CardTransactionAuthorizer", "createAccount", "authorize", "authorize", "authorize", "authorize", "authorize", "getBalance"]
[[], ["B1", 1000], ["B1", 10, 100], ["B1", 10, 110], ["B1", 10, 150], ["B1", 10, 200], ["B1", 10, 300], ["B1"]]
Output: [null, "CREATED", "APPROVED", "APPROVED", "APPROVED", "HIGH_FREQUENCY", "APPROVED", 960]
```
The purchases at 100, 110 and 150 are approved. At 200 all three are still inside the window, so approving a fourth would break the limit. At 300 the window starts at 180, the earlier approvals have expired, and the purchase goes through. Four approvals of 10 leave 960.
```hint Per-account history
Only approvals from the last 120 seconds matter for the fraud rule, and timestamps never go backwards. Pick a structure that lets you add new approvals at one end and drop expired ones from the other.
```
```hint Boundaries and ordering
Trace a purchase made exactly 120 seconds after an earlier approval, and one that is both over the balance and over the rate limit. Check that no decline path changes the balance or the history.
```
### Constraints & Assumptions
- `1 <= len(accountId) <= 20`
- `0 <= initialBalance <= 10^9`
- `0 <= amount <= 10^9`
- `0 <= timestamp <= 10^9`, and timestamps are non-decreasing across `authorize` calls
- At most `10^5` operations in total
- Fraud window: more than 3 approved transactions within 120 seconds triggers `HIGH_FREQUENCY`
### Clarifying Questions to Ask
- Can calls for the same account arrive concurrently, or can I assume one call at a time?
- Do declined attempts need to be logged for audit, or is returning the reason enough?
- Are refunds, reversals or deposits in scope, or does the balance only go down after the account is created?
- Should the window length and the approval limit be fixed for the whole system or configurable per account?
### What a Strong Answer Covers
- The per-account state that has to be stored and how each method looks it up by id.
- The order of the decline checks and a guarantee that no decline changes the balance or the history.
- How the fraud window is maintained over time, including the inclusive 120-second boundary and why only approvals count.
- The time and space cost of each operation, including how large one account's history can grow.
- Edge cases such as unknown accounts in every method, a purchase equal to the balance, zero-amount purchases, several purchases at the same timestamp, and blocking then unblocking.
### Follow-up Questions
- How would you make `authorize` safe when many threads call it for the same account at once?
- Timestamps may now arrive out of order by up to a few seconds. What changes in the fraud check?
- How would you support holds that are authorized now and captured or released later?
- How would you persist the state so a crash neither loses balances nor lets a burst slip past the fraud rule?
Overview: Implement an in-memory card authorizer that tracks account balances and blocked status and approves or declines each purchase with a specific reason. It tests per-account state design, the order of validation checks, and a time-window fraud rule that allows at most three approvals in 120 seconds.