In-Memory Bank System: API, Data Model, Invariants, Semantics, and Edge Cases
Goal
Design and implement an in-memory bank system that supports accounts, deposits, and payments/transfers. The system must be hash-map-based for O(1) average-time lookups and must define behavior for edge cases (timestamps, idempotency, unknown accounts, insufficient funds) and basic concurrency.
API
Interface/class BankSystem with these methods:
-
bool createAccount(long timestamp, String customerId)
-
int deposit(long timestamp, String customerId, int amount)
-
int pay(long timestamp, String sourceAccountId, String targetAccountId, int amount)
Requirements
-
Data model
-
Define account and transaction structures suitable for O(1) lookups via hash maps.
-
Invariants
-
No negative balances.
-
Per-account timestamps are monotonic (reject stale/out-of-order ops).
-
Return semantics
-
Specify what the methods return on success and on failure (e.g., new balance vs error codes).
-
Complexity
-
Time and space complexity for each operation.
-
Edge cases to handle
-
Duplicate or out-of-order timestamps.
-
Insufficient funds.
-
Unknown accounts.
-
Idempotency under retries.
-
Basic concurrency assumptions.
Constraints
-
Use hash maps (not lists) for constant-time account and transaction lookups.
-
The system is in-memory (single process), but may be accessed concurrently by multiple threads.