Design an in-memory transaction service with auto-increment ids and lookup by id
Company: N26
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a `TransactionService` that exposes behavior equivalent to `POST /transactions`. Each transaction has an `amount` and a `timestamp`.
Every successfully created transaction is assigned a unique auto-incrementing id starting at 1. The service should also support retrieving a transaction by its id, returning the stored fields, or `None` if the id does not exist.
Implement the following methods:
- `create_transaction(amount: int, timestamp: int) -> int` creates a new transaction with the given amount and timestamp and returns its newly assigned unique id.
- `get_transaction(transaction_id: int) -> Optional[Dict]` returns the transaction record as a dict `{id, amount, timestamp}` for the given id, or `None` if it does not exist.
Example 1:
```text
Input:
["TransactionService", "create_transaction", "create_transaction", "get_transaction", "get_transaction"]
[[], [100, 1000], [250, 1500], [1], [2]]
Output: [null, 1, 2, {"id": 1, "amount": 100, "timestamp": 1000}, {"id": 2, "amount": 250, "timestamp": 1500}]
Explanation:
svc = TransactionService() # empty store, next id = 1
svc.create_transaction(100, 1000) # -> 1 (stores {id: 1, amount: 100, timestamp: 1000})
svc.create_transaction(250, 1500) # -> 2 (stores {id: 2, amount: 250, timestamp: 1500})
svc.get_transaction(1) # -> {id: 1, amount: 100, timestamp: 1000}
svc.get_transaction(2) # -> {id: 2, amount: 250, timestamp: 1500}
```
Example 2:
```text
Input:
["TransactionService", "get_transaction", "create_transaction", "get_transaction", "get_transaction"]
[[], [42], [500, 2000], [1], [42]]
Output: [null, null, 1, {"id": 1, "amount": 500, "timestamp": 2000}, null]
Explanation:
svc = TransactionService() # empty store
svc.get_transaction(42) # -> None (no transactions yet)
svc.create_transaction(500, 2000) # -> 1
svc.get_transaction(1) # -> {id: 1, amount: 500, timestamp: 2000}
svc.get_transaction(42) # -> None (id 42 was never created)
```
```hint What the caller gets back
Think about what happens to the stored data if a caller modifies the dict that `get_transaction` returns.
```
### Constraints & Assumptions
- `1 <= amount <= 10^9`
- `0 <= timestamp <= 10^12`
- At most `10^5` calls are made across all methods.
### Clarifying Questions to Ask
- Should the service validate the amount and timestamp, and how should a rejected request be reported to the caller?
- Does a rejected create use up an id, or must ids stay gap-free?
- If a client retries the same POST after a timeout, should a second transaction be created?
- Will the service be called from several threads or processes at once?
### What a Strong Answer Covers
- How records are stored and how ids are assigned, starting at 1 and never reused.
- Constant-time lookup, including the `None` result for an id that was never created.
- Whether a caller can change a stored record through the value `get_transaction` returns, and how to prevent it.
- Which inputs count as a successful create, and where validation happens relative to id assignment.
- The time and space cost of each operation.
### Follow-up Questions
- How would you keep id assignment correct when many requests call `create_transaction` at the same time?
- How would you support an idempotency key so that a retried POST does not create a duplicate transaction?
- How would you answer "total amount and count of transactions in the last 60 seconds" without scanning every stored transaction?
- What changes if the transactions must be stored in a database and survive a restart?
Overview: Design a small in-memory service that behaves like POST /transactions: it stores an amount and a timestamp, assigns auto-incrementing ids starting at 1, and looks transactions up by id. It tests basic class design, id assignment, what a lookup should hand back to callers, and constant-time operations.