Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 AAPL order 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.
CommandWhat it does
ADD id delay nameSchedule callback name for time now + delay
TICK dtAdvance the clock by dt, then fire every due timer
CANCEL idRemove 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

  1. ADD id delay name schedules name for time now + delay, where now is the clock's current value.
  2. TICK dt advances the clock by dt, then fires every timer whose scheduled time is at most the new now.
  3. A timer with delay 0 is due immediately and fires as part of its own ADD.
  4. A timer fires at most once.
  5. 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 as int64_t nanoseconds.

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.

  1. CANCEL id removes a pending timer so that it never fires.
  2. An id that was never added, has already fired, or was already cancelled is ignored.
  3. CANCEL prints nothing.

Tests 6–8 cover this part.

Example
Input
5
ADD 8 7 late
ADD 3 2 early
ADD 1 7 same
TICK 10
TICK 0
Output
FIRE 3 early
FIRE 1 same
FIRE 8 late
Explanation

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.