Implement banking ops: transfer, top-k, cashback, merge
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Design and implement an in-memory banking system that supports:
(
1) CreateAccount(id, initialBalance) and Transfer(fromId, toId, amount) with validation for nonnegative amounts, sufficient funds, account existence, idempotency via transaction IDs, and atomic balance updates;
(
2) a query to return the accounts with the top k total outgoing funds across all transfers, specifying output format, tie-breaking, and expected time/space complexity (e.g., maintain running aggregates for O(log n) updates and O(k log n) queries);
(
3) a Purchase(accountId, amount, txnId) that awards 2% cashback credited exactly 24 hours after the purchase, requiring a structure for pending rewards, a scheduler or sweep to post credits, and idempotent, crash-safe processing;
(
4) MergeAccount(primaryId, duplicateId) to combine balances and transaction histories, redirect future operations from duplicateId to primaryId, and reconcile pending cashback and duplicate transaction IDs. Discuss chosen data structures, concurrency assumptions (single-threaded vs. multi-threaded), and provide unit tests for edge cases (insufficient funds, duplicate transfers, simultaneous merges, and time-bound rewards).
Quick Answer: This question evaluates a candidate's ability to design a robust in-memory banking system, covering transactional correctness, idempotency, concurrency control, delayed cashback scheduling, aggregate top-k queries, and data structure and complexity trade-offs within the Coding & Algorithms domain.
Process create, deposit, transfer, cashback, top, and merge operations. Return one output per operation.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: ([['create','a'], ['create','b'], ['deposit','a',100], ['transfer','a','b',40], ['top',2], ['cashback','a',5], ['merge','b','a'], ['top',2]],)
Expected Output: [True, True, 100, True, ['a', 'b'], 65, True, ['a']]
Explanation: Transfer ranking, cashback, merge.
Input: ([['deposit','missing',10], ['create','x'], ['transfer','x','x',1], ['top',3]],)
Expected Output: [None, True, False, ['x']]
Explanation: Invalid cases.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.