Design payment scheduler with cancel and top-K outgoing
Company: Circle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Design and implement an in-memory banking payment component with the following APIs:
(
1) schedulePayment(payerId, payeeId, amount, executeAt) -> paymentId: create a pending outgoing payment scheduled for executeAt.
(
2) cancelPayment(paymentId) -> boolean: cancel only if still pending.
(
3) removePayment(paymentId) -> boolean: allow deletion only if the payment is CANCELED or FAILED; successful payments cannot be removed.
(
4) processDuePayments(now): execute all pending payments with executeAt <= now; mark each as SUCCESS if funds are sufficient, otherwise FAILED; only successful payments contribute to outgoing totals.
(
5) getTopKByOutgoingTotal(k) -> list<accountId>: return accountIds sorted by descending total outgoing amount from successful payments only; break ties by smaller accountId. Requirements: support only outgoing flow tracking (no incoming totals). Define the data structures to support efficient scheduling, cancellation, removal, and top-k queries; provide the time and space complexity of each API. Describe how you handle edge cases such as canceling an already executed payment, repeated cancellations, removing non-existent or ineligible payments, and ensuring that canceled payments are not executed.
Quick Answer: This question evaluates knowledge of designing stateful in-memory services, efficient data structures and algorithms for scheduled task execution and top‑K outgoing aggregation, and correctness in handling cancellation, failure semantics, and related edge cases.