Timer Scheduler
Prerequisites: Time, Timestamps, and Clocks, Order Gateway and Risk Checks
A trading process runs many timers at once. Checking all of them whenever the clock moves costs time in proportion to their number, even when none is due. Build a scheduler that finds the due timers directly and fires them in a fixed order.
- Timer: a callback (a piece of work to run later) plus the time it becomes due. The order gateway's alert when the exchange is late acknowledging an
AAPLorder is one. A retry of a rejected message after a delay is another. - Heartbeat: a short message telling the other side the connection is still alive. The exchange session sends one whenever it has been quiet for a set interval, which is another timer.
What to implement
process_commands(commands)takes one command per line and returns the printed lines, in order.- A callback is represented by its name, which contains no whitespace. Firing it prints a line.
- On stdin, the first line is the number of commands, and each remaining line is one command.
- There are two parts. Part 2 adds one command. One solution must pass all nine tests.
| Command | What it does |
|---|---|
ADD id delay name | Schedule callback name for time now + delay |
TICK dt | Advance the clock by dt, then fire every due timer |
CANCEL id | Remove a pending timer so it never fires (Part 2) |
The scheduler's clock starts at 0 and changes only on TICK, so the same commands always produce the same output. In a running system it is the steady clock from Time, Timestamps, and Clocks, because a timer measures a duration.
Part 1: Schedule and fire
ADD id delay nameschedulesnamefor timenow + delay, wherenowis the clock's current value.TICK dtadvances the clock bydt, then fires every timer whose scheduled time is at most the newnow.- A timer with
delay0 is due immediately and fires as part of its ownADD. - A timer fires at most once.
- Timers fired by one command print in order of scheduled time, then ascending id. The order they were added in does not matter.
Output: one line per fired timer.
FIRE id name
Limits
- Timer ids are signed 32-bit integers, so they can be negative. They are unique and never reused.
- Delays and clock steps are integers from 0 to 1,000,000,000. There are at most 200,000 commands.
- Three maximal steps already pass the largest 32-bit
int. Keep time in a 64-bit integer, for the same reason timestamps are stored asint64_tnanoseconds.
Tests 1–5 and 9 cover this part.
Part 2: Cancel pending work
A timer can become unnecessary before it fires: once the acknowledgment arrives, its alert should not go off.
CANCEL idremoves a pending timer so that it never fires.- An id that was never added, has already fired, or was already cancelled is ignored.
CANCELprints nothing.
Tests 6–8 cover this part.
5 ADD 8 7 late ADD 3 2 early ADD 1 7 same TICK 10 TICK 0
FIRE 3 early FIRE 1 same FIRE 8 late
Timers 8, 3, and 1 are due at times 7, 2, and 7. TICK 10 moves the clock to 10, so all three fire during that one command.
Timer 3 prints first because 2 is the earliest time. Timers 1 and 8 share time 7, so they print in ascending id order, 1 before 8, even though 8 was added first.
TICK 0 leaves the clock at 10, and no timer is left to fire.