Design a transaction processing engine with per-type fees for ACH, wire and card
Company: Brex
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a transaction processing engine that handles three types of financial transactions: ACH, wire and card. Each type has its own fee structure and processing rules.
The fee rules are:
- ACH charges a flat fee of \$0.25 per transaction.
- Wire charges \$15.00 flat plus 0.1% of the amount.
- Card charges 2.9% of the amount plus \$0.30.
The engine must track each processed transaction by a unique id, support retrieving the status, fee and net amount of a transaction, and report the total fees collected per transaction type.
Implement a class `TransactionProcessingEngine` with these methods:
- `process(txn_type: str, amount: float) -> int` processes a transaction of the given type and amount and returns the assigned transaction id.
- `get_status(txn_id: int) -> str` returns the status of the transaction, `"PROCESSED"` or `"NOT_FOUND"`.
- `get_fee(txn_id: int) -> float` returns the fee charged for the transaction, rounded to 2 decimals, or `-1.0` if the id is not found.
- `get_net_amount(txn_id: int) -> float` returns the net amount (amount minus fee), rounded to 2 decimals, or `-1.0` if the id is not found.
- `get_total_fees(txn_type: str) -> float` returns the total fees collected for transactions of the given type, rounded to 2 decimals.
Example 1 covers the flat ACH fee and lookups of an id that does not exist:
```text
Input:
["TransactionProcessingEngine", "process", "process", "get_status", "get_status", "get_fee", "get_total_fees", "get_net_amount"]
[[], ["ACH", 50], ["ACH", 200], [1], [99], [99], ["ACH"], [2]]
Output: [null, 1, 2, "PROCESSED", "NOT_FOUND", -1.0, 0.5, 199.75]
Explanation:
engine = TransactionProcessingEngine() # empty engine
engine.process("ACH", 50.0) # -> 1 (fee = $0.25)
engine.process("ACH", 200.0) # -> 2 (fee = $0.25)
engine.get_status(1) # -> "PROCESSED"
engine.get_status(99) # -> "NOT_FOUND" (id 99 does not exist)
engine.get_fee(99) # -> -1.0 (id 99 does not exist)
engine.get_total_fees("ACH") # -> 0.5 (2 * $0.25)
engine.get_net_amount(2) # -> 199.75 (200 - 0.25)
```
Both ACH transactions pay the same flat \$0.25, so the ACH total is \$0.50. Id 99 was never assigned, so the status lookup and the fee lookup both report it as missing.
Example 2 covers the wire and card fee rules:
```text
Input:
["TransactionProcessingEngine", "process", "process", "process", "get_fee", "get_net_amount", "get_total_fees", "get_total_fees", "get_status"]
[[], ["WIRE", 10000], ["CARD", 100], ["CARD", 25.5], [1], [3], ["CARD"], ["ACH"], [3]]
Output: [null, 1, 2, 3, 25.0, 24.46, 4.24, 0.0, "PROCESSED"]
Explanation:
engine = TransactionProcessingEngine()
engine.process("WIRE", 10000.0) # -> 1 (fee = 15.00 + 10.00 = $25.00)
engine.process("CARD", 100.0) # -> 2 (fee = 2.90 + 0.30 = $3.20)
engine.process("CARD", 25.5) # -> 3 (fee = 0.7395 + 0.30 = 1.0395, rounds to $1.04)
engine.get_fee(1) # -> 25.0
engine.get_net_amount(3) # -> 24.46 (25.50 - 1.04)
engine.get_total_fees("CARD") # -> 4.24 (3.20 + 1.04)
engine.get_total_fees("ACH") # -> 0.0 (no ACH transactions)
engine.get_status(3) # -> "PROCESSED"
```
The wire fee is \$15.00 plus 0.1% of 10,000, which is \$10.00. The third card fee is 1.0395 before rounding, which becomes 1.04 under any common rounding rule, so the card total is 3.20 + 1.04 = 4.24.
```hint Shape of the fee rules
Write the three fee formulas side by side and see what they have in common before deciding how many classes you need.
```
```hint Money values
Decide where rounding to cents happens, and check what binary floats do to values like 0.1 or 2.675 before you keep a running total in them.
```
### Constraints & Assumptions
- `txn_type` is one of `"ACH"`, `"WIRE"`, `"CARD"`.
- `0 < amount <= 10^9`.
- At most `10^4` calls are made to each method.
- Transaction ids are assigned starting at 1 and increase by 1.
### Clarifying Questions to Ask
- When a fee lands on a fraction of a cent, which rounding rule applies, and are the net amount and the fee totals computed from the rounded fee or from the exact one?
- Can an amount carry more than two decimal places?
- Can a transaction end up in a state other than PROCESSED, such as failed or reversed, or is that out of scope?
- Will new transaction types or fee rules be added later, such as tiered or capped fees?
- Should a transaction be rejected when its fee is larger than its amount, as with a \$10 wire?
### What a Strong Answer Covers
- A fee calculation per transaction type that can take a new type or rule without changing the processing logic.
- How transactions are stored, how ids are assigned, and how each lookup handles an unknown id.
- A stated rounding policy and a money representation that does not pick up binary floating point error in running totals.
- How the per-type totals stay correct without scanning every transaction.
- The time and space cost of each operation.
### Follow-up Questions
- How would you add a capped card fee or a tiered wire fee without editing the existing fee classes?
- How would you support refunds or failed transactions, and what should happen to the fee totals when a transaction is reversed?
- How would you make `process` safe under concurrent calls, and how would you stop a retried request from charging twice?
- If the engine had to survive a restart, what would you persist and how would you rebuild the per-type totals?
Overview: Design a transaction processing engine in Python that charges different fees for ACH, wire and card payments, stores each transaction by id, and reports fee totals per type. It tests class design that stays open to new fee rules, careful handling of money and rounding, and constant-time lookups.