Design a transaction history service with balance, recent and time-range queries
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a transaction history service that records financial transactions for user accounts and supports efficient querying.
Each transaction has a unique `transactionId`, an `accountId`, a `timestamp`, a `type` (`DEPOSIT` or `WITHDRAWAL`), and an `amount`. Transactions for a given account are always recorded in strictly increasing `timestamp` order.
The service should support recording a transaction, fetching the current balance of an account, retrieving the most recent transactions, querying transactions within a time range, and computing the total deposited or withdrawn amount over a period.
Implement the following methods:
- `recordTransaction(transactionId: int, accountId: int, timestamp: int, type: str, amount: int)`: record a new transaction for the given account. Deposits add to the balance; withdrawals subtract.
- `getBalance(accountId: int) -> int`: return the current balance of the account. Returns 0 if the account has no transactions.
- `getRecentTransactions(accountId: int, k: int) -> List[int]`: return the transaction IDs of the most recent `k` transactions for the account, ordered from most recent to oldest.
- `getTransactionsInRange(accountId: int, startTime: int, endTime: int) -> List[int]`: return the transaction IDs whose timestamps fall in `[startTime, endTime]`, in chronological order.
- `getTotalInRange(accountId: int, type: str, startTime: int, endTime: int) -> int`: return the sum of amounts for transactions of the given type whose timestamps fall in `[startTime, endTime]`.
For example, after the five `recordTransaction` calls below, the queries return the values on the right:
```text
recordTransaction(1, 100, 10, "DEPOSIT", 500)
recordTransaction(2, 100, 20, "WITHDRAWAL", 200)
recordTransaction(3, 200, 25, "DEPOSIT", 50)
recordTransaction(4, 100, 30, "DEPOSIT", 100)
recordTransaction(5, 100, 40, "WITHDRAWAL", 600)
getBalance(100) -> -200
getBalance(300) -> 0
getRecentTransactions(100, 2) -> [5, 4]
getRecentTransactions(200, 5) -> [3]
getTransactionsInRange(100, 20, 40) -> [2, 4, 5]
getTotalInRange(100, "WITHDRAWAL", 15, 40) -> 800
getTotalInRange(100, "DEPOSIT", 11, 29) -> 0
```
Account 100 ends at ${500 - 200 + 100 - 600 = -200}$, which is allowed. Account 300 has never transacted, so its balance is 0. Both ends of a range are inclusive, so the window from 20 to 40 includes transactions 2 and 5, which sit exactly on its edges.
```hint Use the ordering guarantee
Timestamps for one account always arrive in increasing order. Think about what that gives you when you append, and how it lets you find the edges of a time window without scanning the whole history.
```
```hint Totals over a window
getTotalInRange returns a single number. Ask whether its cost has to grow with the number of transactions inside the window, or whether something kept up to date at record time could answer it directly.
```
### Constraints & Assumptions
- `1 <= accountId, transactionId <= 10⁹`
- `1 <= timestamp <= 10¹²`, strictly increasing per `accountId`
- `type` is either `"DEPOSIT"` or `"WITHDRAWAL"`
- `1 <= amount <= 10⁶`
- At most `10⁵` calls in total across all methods
- Withdrawals are allowed even if they cause a negative balance
### Clarifying Questions to Ask
- Is a repeated `transactionId` guaranteed never to happen, or should the service reject or ignore it?
- What should `getRecentTransactions` return when `k` is 0 or larger than the account's history?
- Is the service called from a single thread, or must recording and querying be safe at the same time?
- Which calls dominate the workload: recording, or range and total queries?
### What a Strong Answer Covers
- A per-account layout that keeps transactions in time order, and why the ordering guarantee means plain appends are enough.
- The cost of every method, with `getBalance` in constant time and unknown accounts returning 0 or an empty list without side effects.
- How the window edges are found with both ends inclusive, including the case where `startTime` is greater than `endTime`.
- How `getTotalInRange` stays fast when ${10^5}$ calls hit one large account, and the extra memory that costs.
- Edge cases for `k` (zero, larger than the history), negative balances, and integer widths for timestamps and sums.
### Follow-up Questions
- Timestamps for an account can now arrive out of order, for example late events from another system. What changes in storage and in each query?
- How would you make the service safe when many threads record and query at once?
- A transaction sometimes needs to be reversed. How would you support that without breaking the history or the range totals?
- Recording the same `transactionId` twice should have no effect. How would you add that?
Overview: Design an in-memory transaction history service that records deposits and withdrawals per account and answers balance, most recent k, time range and per-type total queries. It tests how you lay out per-account data, find time windows quickly, handle inclusive bounds and edge cases, and state the cost of each method.