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

Conflating Queue

Prerequisites: Packet Drops and Backpressure, SPSC Ring Buffer Handoff

A feed thread decodes quotes for many symbols and hands them to a strategy thread that falls behind during a burst. Build a conflating queue between them.

  • FIFO: a first-in, first-out queue. With a plain FIFO, the strategy works through every quote in arrival order, including AAPL quotes already replaced by a newer AAPL quote.
  • Conflation: keeping only the latest quote per symbol, the remedy Packet Drops and Backpressure gives for a consumer that cannot keep up.
  • Pending: a symbol is pending from the update that puts it in the FIFO until a poll delivers it.
  • Conflating queue: a FIFO of pending symbols plus each pending symbol's latest quote.

What to implement

  • process_commands(capacity, policy, commands) returns the printed lines, in command order. Each command is one line.
  • capacity is the most symbols that may be pending at once. policy is DROP_NEW or EVICT_OLDEST. Both matter only from Part 2 on.
  • On stdin, the first two lines are capacity and policy, and the third is the number of commands.
  • There are three parts, each adding behavior on top of the previous ones. One solution must pass all ten tests.
CommandMeaningPrints
UPDATE symbol bid askA new quote for symbol from the feednothing
POLLThe strategy takes the oldest pending symbolsymbol bid ask, or EMPTY
PENDINGReport the queue depthPENDING n
CONFLATEDReport how many updates were overwrittenCONFLATED c
DROPPEDReport how many quotes the capacity limit discarded (Part 2)DROPPED d
POLL_ALLThe strategy takes every pending symbol at once (Part 3)BATCH n, then n quote lines

Part 1: Conflate in place

  1. UPDATE symbol bid ask for a symbol that is not pending appends symbol to the back of the FIFO and stores bid ask as its quote.
  2. UPDATE for a symbol that is already pending replaces the stored quote with bid ask, leaves the symbol's FIFO position unchanged, and adds 1 to the conflated count, even when the new prices equal the old ones. The overwritten quote is never delivered.
  3. POLL removes the front symbol and prints it with its latest quote. With nothing pending, it prints EMPTY and changes nothing.
  4. A polled symbol is no longer pending, so its next UPDATE appends it at the back again instead of conflating.

Output

symbol bid ask
EMPTY
PENDING n
CONFLATED c
  • n = the number of pending symbols.
  • c = the overwrites since the start: stale quotes a plain FIFO would have delivered.

Limits

  • Symbols are 1 to 8 uppercase letters, such as AAPL.
  • bid and ask are integer prices in ticks between 1 and 109. The queue stores and prints them as given and never compares them.
  • capacity is between 1 and 105. There are at most 105 commands.
  • Every count fits in a 32-bit int.

Tests 1–4 cover this part. They use capacity 100, policy DROP_NEW, and at most five symbols, so the limit never applies.

Part 2: Bound the pending symbols

The FIFO now holds at most capacity symbols.

  1. An UPDATE for a pending symbol always conflates as in Part 1, even when the FIFO is full, because it needs no new place.
  2. An UPDATE for a symbol that is not pending, arriving while the FIFO is full, is handled by policy (table below).
  3. Under either policy one quote is lost, and the dropped count grows by 1.
  4. An evicted quote counts as one drop even if it had absorbed several updates, because those updates were already counted as conflated.
  5. A symbol whose update was dropped, or that was evicted, is not pending, so its next UPDATE is treated as new.
policyEffect of an update for a new symbol when the FIFO is full
DROP_NEWDiscard the update. The FIFO and every stored quote stay as they were.
EVICT_OLDESTRemove the front symbol and discard its quote unprinted. Then append the new symbol with the update's quote.

Output: DROPPED d, where d is the number of quotes lost to the limit since the start.

Every UPDATE now ends in exactly one place: overwritten, delivered, dropped, or still pending. As a check for your traces, after any command:

UPDATE commands so far = conflated count + quotes delivered + dropped count + pending symbols

Tests 5–8 cover this part. Tests 5 and 8 use DROP_NEW, tests 6 and 7 use EVICT_OLDEST, and test 7 has a capacity of 1.

Part 3: Drain in one batch

A strategy waking after a stall often wants every pending symbol at once.

  1. POLL_ALL delivers every pending symbol and leaves the queue empty. With nothing pending it prints BATCH 0 and nothing else.
  2. As with POLL, delivered symbols are no longer pending, so a later UPDATE for any of them appends instead of conflating.

Output

BATCH n
symbol bid ask
  • n = the number of pending symbols.
  • Then n lines in the POLL format, one per symbol in FIFO order, oldest first.

Tests 9–10 cover this part.

Example
Input
100
DROP_NEW
13
UPDATE AAPL 19010 19012
UPDATE MSFT 41250 41252
UPDATE AAPL 19011 19013
PENDING
UPDATE AAPL 19012 19014
POLL
UPDATE MSFT 41251 41253
UPDATE AAPL 19013 19015
POLL
POLL
POLL
PENDING
CONFLATED
Output
PENDING 2
AAPL 19012 19014
MSFT 41251 41253
AAPL 19013 19015
EMPTY
PENDING 0
CONFLATED 3
Explanation

AAPL is appended first and MSFT second. The third command finds AAPL pending and overwrites its quote in place, so PENDING reports 2 symbols although 3 quotes have arrived.

The fifth command overwrites AAPL again. The first POLL delivers AAPL with its latest quote, 19012 19014.

MSFT 41251 41253 is the third overwrite. The next AAPL update finds AAPL no longer pending and appends it behind MSFT.

The next two polls deliver MSFT and AAPL, and the last finds the queue empty. A plain FIFO would have delivered all six quotes, three of them already replaced.