Design a payment scheduler that fires due payments in time order, with cancellation

Quick Overview

Design a payment scheduler class that executes future payments in order of execution time and id as a logical clock advances, lets pending payments be cancelled, and rejects duplicate ids and past timestamps. It tests priority queues, lazy deletion and careful handling of clock edge cases.

Design a payment scheduler that fires due payments in time order, with cancellation

Company: Brex

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a payment scheduler that triggers future payments at the right time. Each payment has a unique `paymentId`, a `payer`, a `payee`, an `amount`, and an `executeAt` timestamp at which it should fire. The scheduler keeps a monotonic logical clock that starts at 0. When the clock advances to time `t`, every pending payment with `executeAt <= t` must be executed in non-decreasing order of `executeAt`, with ties broken by ascending `paymentId`. Payments can be cancelled while they are still pending. Duplicate `paymentId`s and payments scheduled in the past must be rejected. Implement these methods: - `schedule(paymentId: int, payer: str, payee: str, amount: int, executeAt: int) -> bool` schedules a new payment. It returns `True` on success, or `False` if `paymentId` already exists or `executeAt` is before the current time. - `cancel(paymentId: int) -> bool` cancels a pending payment. It returns `True` if the payment was pending and is now cancelled, `False` otherwise. - `advanceTime(newTime: int) -> List[int]` advances the clock to `newTime` and executes every pending payment with `executeAt <= newTime`. It returns the executed ids in `(executeAt, paymentId)` order. - `getExecuted() -> List[int]` returns the ids of all payments executed so far, in execution order. - `getPending() -> List[int]` returns the ids of all pending payments, sorted by `(executeAt, paymentId)`. These rules make every result unique: - A `paymentId` exists once `schedule` has accepted it, whether that payment is still pending, has executed, or was cancelled. A call that returns `False` changes nothing. - An `executeAt` equal to the current time is not in the past. That payment is accepted and stays pending until the next `advanceTime` call, which may pass the same time. - If `newTime` is earlier than the current time, `advanceTime` leaves the clock where it is, executes nothing, and returns `[]`. Example 1, with two payments due at the same time: ```text scheduler = PaymentScheduler() # clock = 0 scheduler.schedule(1, "alice", "bob", 100, 10) -> True scheduler.schedule(2, "carol", "dave", 50, 5) -> True scheduler.schedule(3, "eve", "frank", 75, 10) -> True scheduler.advanceTime(10) -> [2, 1, 3] scheduler.getExecuted() -> [2, 1, 3] scheduler.getPending() -> [] ``` All three payments are due by time 10. Payment 2 fires first because it is due at 5. Payments 1 and 3 share time 10, so the smaller id goes first. Example 2, with a cancellation and a payment in the past: ```text scheduler = PaymentScheduler() # clock = 0 scheduler.schedule(10, "x", "y", 200, 20) -> True scheduler.schedule(11, "x", "z", 300, 15) -> True scheduler.cancel(10) -> True scheduler.advanceTime(15) -> [11] scheduler.schedule(12, "a", "b", 99, 10) -> False scheduler.getExecuted() -> [11] scheduler.getPending() -> [] ``` Payment 10 was cancelled, so only 11 fires at time 15. Payment 12 asks for time 10 while the clock reads 15, so it is rejected and nothing is left pending. Example 3, with id reuse, a payment due at the current time, and a backward clock move: ```text scheduler = PaymentScheduler() # clock = 0 scheduler.schedule(7, "ann", "ben", 40, 30) -> True scheduler.advanceTime(30) -> [7] scheduler.schedule(7, "ann", "ben", 40, 50) -> False scheduler.schedule(8, "cat", "dan", 25, 30) -> True scheduler.schedule(9, "cat", "eli", 60, 45) -> True scheduler.cancel(9) -> True scheduler.cancel(9) -> False scheduler.advanceTime(20) -> [] scheduler.getPending() -> [8] scheduler.advanceTime(30) -> [8] scheduler.getExecuted() -> [7, 8] ``` Id 7 already belongs to an executed payment, so the second `schedule(7, ...)` fails. Payment 8 is due exactly at the current time 30, so it is accepted and fires on the next `advanceTime(30)`. The second `cancel(9)` fails because 9 is no longer pending, and `advanceTime(20)` does nothing because it would move the clock backward. ```hint Finding the next payment Each clock advance needs the smallest `(executeAt, paymentId)` among the pending payments, again and again, while new payments keep arriving. Pick a structure that gives you that minimum cheaply without sorting everything. ``` ```hint Cheap cancellation Removing an arbitrary element from the middle of a priority queue is slow in most standard libraries. Ask whether the entry has to leave the queue right away, or whether you can recognize it as dead later. ``` ### Constraints & Assumptions - `1 <= paymentId <= 10^9` - `1 <= amount <= 10^9` - `0 <= executeAt, newTime <= 10^18` - `payer` and `payee` are non-empty strings of length at most 32. - At most `10^5` calls are made across all methods. ### Clarifying Questions to Ask - Does executing a payment here only mean recording it, or does it call a payment processor that can fail? - Can `schedule`, `cancel` and `advanceTime` be called concurrently, or always one at a time? - How often are `getPending` and `getExecuted` called compared with the other methods? That decides whether they can afford to sort or copy on every call. - Should the scheduler keep the full payment record after execution, for example as an audit trail, or are the ids enough? ### What a Strong Answer Covers - A structure that yields due payments in `(executeAt, paymentId)` order without re-sorting all pending payments on every clock advance. - How a cancelled payment is kept from executing, and what cancellation costs. - Which ids count as taken, including the ids of executed and cancelled payments, and the boundary where `executeAt` equals the current time. - What `advanceTime` does for a backward move and for a repeat of the current time. - Time and space per method, including any entries a cancellation leaves behind. ### Follow-up Questions - Executing a payment now calls an external processor that can time out. How do you retry without paying twice? - Most payments are cancelled long before they are due. How do you keep memory bounded? - How would you run the scheduler on several machines so that each payment executes exactly once, even after a crash? - `getPending` is now called after every operation. What would you change?

Overview: Design a payment scheduler class that executes future payments in order of execution time and id as a logical clock advances, lets pending payments be cancelled, and rejects duplicate ids and past timestamps. It tests priority queues, lazy deletion and careful handling of clock edge cases.

Brex logo
Brex
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

Design a payment scheduler that triggers future payments at the right time. Each payment has a unique paymentId, a payer, a payee, an amount, and an executeAt timestamp at which it should fire.

The scheduler keeps a monotonic logical clock that starts at 0. When the clock advances to time t, every pending payment with executeAt <= t must be executed in non-decreasing order of executeAt, with ties broken by ascending paymentId. Payments can be cancelled while they are still pending. Duplicate paymentIds and payments scheduled in the past must be rejected.

Implement these methods:

  • schedule(paymentId: int, payer: str, payee: str, amount: int, executeAt: int) -> bool schedules a new payment. It returns True on success, or False if paymentId already exists or executeAt is before the current time.
  • cancel(paymentId: int) -> bool cancels a pending payment. It returns True if the payment was pending and is now cancelled, False otherwise.
  • advanceTime(newTime: int) -> List[int] advances the clock to newTime and executes every pending payment with executeAt <= newTime . It returns the executed ids in (executeAt, paymentId) order.
  • getExecuted() -> List[int] returns the ids of all payments executed so far, in execution order.
  • getPending() -> List[int] returns the ids of all pending payments, sorted by (executeAt, paymentId) .

These rules make every result unique:

  • A paymentId exists once schedule has accepted it, whether that payment is still pending, has executed, or was cancelled. A call that returns False changes nothing.
  • An executeAt equal to the current time is not in the past. That payment is accepted and stays pending until the next advanceTime call, which may pass the same time.
  • If newTime is earlier than the current time, advanceTime leaves the clock where it is, executes nothing, and returns [] .

Example 1, with two payments due at the same time:

scheduler = PaymentScheduler()                   # clock = 0
scheduler.schedule(1, "alice", "bob", 100, 10)   -> True
scheduler.schedule(2, "carol", "dave", 50, 5)    -> True
scheduler.schedule(3, "eve", "frank", 75, 10)    -> True
scheduler.advanceTime(10)                        -> [2, 1, 3]
scheduler.getExecuted()                          -> [2, 1, 3]
scheduler.getPending()                           -> []

All three payments are due by time 10. Payment 2 fires first because it is due at 5. Payments 1 and 3 share time 10, so the smaller id goes first.

Example 2, with a cancellation and a payment in the past:

scheduler = PaymentScheduler()                   # clock = 0
scheduler.schedule(10, "x", "y", 200, 20)        -> True
scheduler.schedule(11, "x", "z", 300, 15)        -> True
scheduler.cancel(10)                             -> True
scheduler.advanceTime(15)                        -> [11]
scheduler.schedule(12, "a", "b", 99, 10)         -> False
scheduler.getExecuted()                          -> [11]
scheduler.getPending()                           -> []

Payment 10 was cancelled, so only 11 fires at time 15. Payment 12 asks for time 10 while the clock reads 15, so it is rejected and nothing is left pending.

Example 3, with id reuse, a payment due at the current time, and a backward clock move:

scheduler = PaymentScheduler()                   # clock = 0
scheduler.schedule(7, "ann", "ben", 40, 30)      -> True
scheduler.advanceTime(30)                        -> [7]
scheduler.schedule(7, "ann", "ben", 40, 50)      -> False
scheduler.schedule(8, "cat", "dan", 25, 30)      -> True
scheduler.schedule(9, "cat", "eli", 60, 45)      -> True
scheduler.cancel(9)                              -> True
scheduler.cancel(9)                              -> False
scheduler.advanceTime(20)                        -> []
scheduler.getPending()                           -> [8]
scheduler.advanceTime(30)                        -> [8]
scheduler.getExecuted()                          -> [7, 8]

Id 7 already belongs to an executed payment, so the second schedule(7, ...) fails. Payment 8 is due exactly at the current time 30, so it is accepted and fires on the next advanceTime(30). The second cancel(9) fails because 9 is no longer pending, and advanceTime(20) does nothing because it would move the clock backward.

Constraints & Assumptions

  • 1 <= paymentId <= 10^9
  • 1 <= amount <= 10^9
  • 0 <= executeAt, newTime <= 10^18
  • payer and payee are non-empty strings of length at most 32.
  • At most 10^5 calls are made across all methods.

Clarifying Questions to Ask Guidance

  • Does executing a payment here only mean recording it, or does it call a payment processor that can fail?
  • Can schedule , cancel and advanceTime be called concurrently, or always one at a time?
  • How often are getPending and getExecuted called compared with the other methods? That decides whether they can afford to sort or copy on every call.
  • Should the scheduler keep the full payment record after execution, for example as an audit trail, or are the ids enough?

What a Strong Answer Covers Guidance

  • A structure that yields due payments in (executeAt, paymentId) order without re-sorting all pending payments on every clock advance.
  • How a cancelled payment is kept from executing, and what cancellation costs.
  • Which ids count as taken, including the ids of executed and cancelled payments, and the boundary where executeAt equals the current time.
  • What advanceTime does for a backward move and for a repeat of the current time.
  • Time and space per method, including any entries a cancellation leaves behind.

Follow-up Questions Guidance

  • Executing a payment now calls an external processor that can time out. How do you retry without paying twice?
  • Most payments are cancelled long before they are due. How do you keep memory bounded?
  • How would you run the scheduler on several machines so that each payment executes exactly once, even after a crash?
  • getPending is now called after every operation. What would you change?