PayPalCoding·45 minFree preview
Banking System Operations
Implement createAccount / deposit / transfer plus getTopSpenders(n) (top n by cumulative outgoing transfers, ties broken lexicographically by accountId). Stretch goal: schedulePayment with a delay and cancelPayment by id. Scheduled payments execute by due time, not creation order, so a plain FIFO queue is the wrong structure.
SWE
simulation
object-design
hashmap
heap
sorting
scheduling
payment
medium
Frequency
Single report
Last asked
2025-11-20
Stage
onsite-coding
Problem
Simulate a simple banking system. Every operation carries a timestamp.
createAccount(accountId: str, timestamp: int)
deposit(accountId: str, amount: int, timestamp: int)
transfer(fromId: str, toId: str, amount: int, timestamp: int)
getTopSpenders(n: int, timestamp: int) -> List[Tuple[str, int]]getTopSpendersreturns the topnaccounts by cumulative transferred-out amount up totimestamp.- Each output item is
(accountId, spendAmount), sorted descending byspendAmount; ties are sorted lexicographically byaccountId.
Stretch goal
schedulePayment(accountId: str, amount: int, delay: int, timestamp: int) -> paymentId
cancelPayment(paymentId: str, timestamp: int) -> boolschedulePaymentschedules deductingamountfromaccountIdattimestamp + delayand returns a payment id.cancelPaymentcancels the scheduled payment if it has not executed yet; returnstrueon success,falseotherwise.
Hints
Pitfalls
Data-structure choice
- Proposing a plain queue instead of a priority queue for
schedulePaymentis a likely rejection trigger. Payments with different delays do not come due in the order they were created, so pending payments must be ordered by execution time.
Time management
- The base problem is easy, but 45 minutes is tight: finishing the four base operations with tests can leave only enough time for pseudocode on the stretch goal. Move quickly through the base operations to leave real time for scheduling and cancellation.
Concurrency
- Unordered per-account locking deadlocks on simultaneous A to B and B to A transfers; a negative amount silently reverses a transfer.
Unspecified behavior to clarify
- What
deposit/transfershould return or do for a missing account or insufficient balance. - When due scheduled payments execute relative to the operation whose timestamp reaches them, and whether an executed scheduled payment counts toward
getTopSpenders.
Interviewer behavior
- Interviewers may keep the camera off and give little feedback. Narrate design decisions and test cases unprompted.
Talking Points
- Say the lock-ordering rule (sorted account id) before writing code.
- Snapshot accounts under a lock for
getTopSpenders; sort by(-total, account_id)for deterministic output. - State up front whether invalid operations return
Noneor raise, and stay consistent. - Map lock and per-account locks guard different things; validate
amount >= 0.
Follow-ups
Add
schedulePayment with a delay and return a payment id.Add
cancelPayment that only succeeds for payments not yet executed.Preparation
- Implement the four base operations plus unit tests in under 25 minutes, including the descending-amount / lexicographic tie-break ordering.
- Implement scheduled payments with a min-heap keyed by execution time, plus a cancelled-id set or status map, and process due payments before each operation.
- Write test cases for cancelling an already-executed payment, cancelling twice, and two payments due at the same timestamp.
- Write the locked
transferfrom memory, including the sorted lock order.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
