This question evaluates data modeling, state management, transaction correctness, event scheduling, and algorithmic ranking competencies needed for building an in-memory transactional system with merge semantics and time-based operations.
Implement an in-memory banking system that manages user accounts, transfers, rankings by spending, scheduled payments, and account merging.
Design a class/module with methods to support the following features (you may choose exact method signatures, but behavior must be clear and testable):
addUser(userId)
: Create a new user/account with zero balance.
deposit(userId, amount)
: Add funds to a user’s balance.
transfer(fromUserId, toUserId, amount)
: Move funds between two users if the sender has sufficient balance.
topSpenders(k)
that returns the top
k
users ranked by total spending (ties broken deterministically, e.g., by userId).
schedulePayment(fromUserId, toUserId, amount, executeAtTimestamp)
.
cancelPayment(paymentId)
(only if it has not executed yet).
processDuePayments(currentTimestamp)
call).
mergeUsers(sourceUserId, targetUserId)
that merges
sourceUserId
into
targetUserId
.
Assume all amounts are positive integers. Define clear return values for success/failure cases (e.g., boolean, exceptions, or error codes).