Design payment scheduler with cancel and top-K outgoing
Company: Circle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
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.
Constraints
- 0 <= len(balances) <= 2 * 10^5
- 0 <= len(operations) <= 2 * 10^5
- Account IDs are positive integers
- Initial balances are non-negative integers
- 1 <= amount <= 10^9
- 0 <= executeAt, now <= 10^9
- 0 <= k <= 2 * 10^5
Examples
Input: ({1: 100, 2: 50, 3: 0}, [('schedule', 1, 2, 70, 10), ('schedule', 1, 3, 40, 5), ('schedule', 2, 3, 60, 5), ('cancel', 1), ('process', 5), ('topk', 2), ('remove', 3), ('process', 10), ('cancel', 1), ('topk', 3)])
Expected Output: [1, 2, 3, True, 2, [1], True, 0, False, [1]]
Explanation: Payment 1 is canceled before execution. At time 5, payment 2 succeeds and payment 3 fails due to insufficient funds. Only account 1 has successful outgoing total, failed payment 3 can be removed, and the canceled payment is skipped later.
Input: ({1: 100, 2: 0, 3: 0, 4: 0}, [('schedule', 1, 2, 50, 7), ('schedule', 1, 3, 50, 7), ('schedule', 1, 4, 10, 7), ('process', 7), ('topk', 3), ('cancel', 2), ('remove', 2)])
Expected Output: [1, 2, 3, 3, [1], False, False]
Explanation: All three payments are due at the same time, so they are processed by paymentId order: 1, 2, then 3. The first two succeed and use up all funds; the third fails. A successful payment cannot be canceled or removed.
Hints
- Store each payment by paymentId in a hash map so cancel, remove, and status checks are O(1).
- A min-heap ordered by (executeAt, paymentId) is useful for due payments. For top-k outgoing totals, consider a max-heap with lazy deletion because totals only increase after successful payments.