Design an in-memory transaction store with lookup, count and bulk delete
Company: N26
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design an in-memory transaction store that records financial transactions and supports a bulk delete operation.
The store must expose an endpoint-like method `deleteAll` that mimics `DELETE /transactions`: it discards every existing transaction, so every read after it sees an empty store.
Each transaction is identified by a unique integer `id` and has an associated `amount`.
Implement the following methods:
- `add(id: int, amount: int)`: insert or overwrite a transaction with the given id and amount.
- `get(id: int) -> int`: return the amount stored for the given transaction id, or -1 if no such transaction exists.
- `count() -> int`: return the number of transactions currently stored.
- `deleteAll()`: discard all existing transactions. After this call the store is empty.
Example 1. The first input list names the calls, the second gives their arguments, and the output lists each call's return value.
```text
Input:
["TransactionStore", "add", "add", "count", "deleteAll", "count", "get"]
[[], [1, 100], [2, 250], [], [], [], [1]]
Output: [null, null, null, 2, null, 0, -1]
store = TransactionStore() # empty store
store.add(1, 100) # transactions: {1: 100}
store.add(2, 250) # transactions: {1: 100, 2: 250}
store.count() # returns 2
store.deleteAll() # transactions: {} (all discarded)
store.count() # returns 0
store.get(1) # returns -1 (transaction was deleted)
```
Example 2:
```text
Input:
["TransactionStore", "deleteAll", "add", "get", "deleteAll", "add", "count"]
[[], [], [5, -75], [5], [], [9, 1000], []]
Output: [null, null, null, -75, null, null, 1]
store = TransactionStore() # empty store
store.deleteAll() # no-op on an empty store
store.add(5, -75) # transactions: {5: -75}
store.get(5) # returns -75
store.deleteAll() # transactions: {}
store.add(9, 1000) # transactions: {9: 1000}
store.count() # returns 1
```
```hint Cost of a bulk delete
Work out what actually happens to the old entries when `deleteAll` runs with `10⁵` transactions stored, and which earlier calls that work can be charged to.
```
### Constraints & Assumptions
- `1 <= id <= 10⁶`
- `-10⁹ <= amount <= 10⁹`
- At most `10⁵` operations will be performed.
- All `id` values passed to `add` are unique. The overwrite behavior of `add` should still be correct if an id repeats.
### Clarifying Questions to Ask
- Amounts can be negative, so a stored amount of -1 is possible. Is it acceptable that `get` returns the same -1 for a missing id, or should a missing id be signaled another way?
- Does `deleteAll` need to run in constant time in the worst case, or is constant amortized time enough?
- Will several threads call the store at the same time?
- Must deleted transactions be gone for good, or kept somewhere for audit while reads see an empty store?
### What a Strong Answer Covers
- A data structure that gives constant-time `add`, `get` and `count`, with an overwrite leaving `count` unchanged.
- An honest cost analysis of `deleteAll` in the chosen language: what reassigning or clearing the container does to the old entries, and the worst-case versus amortized bound.
- The edge cases: `deleteAll` on an empty store, `get` after a delete, negative amounts and the ambiguity of the -1 sentinel.
- Space usage, and when memory is released after a bulk delete.
### Follow-up Questions
- How would you make `deleteAll` constant time in the worst case, without freeing every entry during the call?
- What changes if `add`, `get` and `deleteAll` run concurrently on several threads?
- Financial records are often kept for audit. How would you keep deleted transactions recoverable while reads still see an empty store?
- How would you add a `sum()` method that runs in constant time, and what integer width does it need?
Overview: Design an in-memory store for financial transactions with add, get, count and a bulk delete that mirrors DELETE /transactions. It tests hash map design, an honest worst-case and amortized cost analysis of clearing a container, and edge cases such as overwrites and negative amounts.