This question evaluates the ability to design and implement a stateful in-memory banking system, testing skills in data structures, algorithms, API design, transaction semantics, scheduling, and correctness under edge cases within the coding and algorithms domain, and it focuses primarily on practical application with conceptual system-design considerations. It is commonly asked to assess reasoning about state management, invariants, error handling, efficiency (e.g., top-k ranking), time-based scheduled events and cancellation semantics, and preserving operation history during merges, highlighting attention to edge cases and performance trade-offs.
Implement an in-memory banking system (e.g., in Java) that supports the following operations on user accounts. Assume user IDs are unique strings and monetary amounts are positive integers.
Core requirements:
addUser(userId)
: create a new user/account.
deposit(userId, amount)
: add funds to a user.
transfer(fromUserId, toUserId, amount)
: move funds between users.
getTopSpenders(k)
that returns the top
k
accounts ranked by their
total spending
.
schedulePayment(fromUserId, toUserId, amount, executeAtTimestamp)
creates a future payment and returns a
paymentId
.
cancelPayment(paymentId)
cancels a scheduled payment if it has not executed yet.
processPaymentsUpTo(timestamp)
), executing all payments whose execution time is reached.
mergeUsers(sourceUserId, targetUserId)
merges
source
into
target
.
You may assume single-threaded execution unless otherwise specified. Design appropriate data structures and APIs, and ensure correctness for edge cases.