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
AAPLquotes already replaced by a newerAAPLquote. - 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.capacityis the most symbols that may be pending at once.policyisDROP_NEWorEVICT_OLDEST. Both matter only from Part 2 on.- On stdin, the first two lines are
capacityandpolicy, 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.
| Command | Meaning | Prints |
|---|---|---|
UPDATE symbol bid ask | A new quote for symbol from the feed | nothing |
POLL | The strategy takes the oldest pending symbol | symbol bid ask, or EMPTY |
PENDING | Report the queue depth | PENDING n |
CONFLATED | Report how many updates were overwritten | CONFLATED c |
DROPPED | Report how many quotes the capacity limit discarded (Part 2) | DROPPED d |
POLL_ALL | The strategy takes every pending symbol at once (Part 3) | BATCH n, then n quote lines |
Part 1: Conflate in place
UPDATE symbol bid askfor a symbol that is not pending appendssymbolto the back of the FIFO and storesbid askas its quote.UPDATEfor a symbol that is already pending replaces the stored quote withbid 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.POLLremoves the front symbol and prints it with its latest quote. With nothing pending, it printsEMPTYand changes nothing.- A polled symbol is no longer pending, so its next
UPDATEappends 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. bidandaskare integer prices in ticks between 1 and 109. The queue stores and prints them as given and never compares them.capacityis 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.
- An
UPDATEfor a pending symbol always conflates as in Part 1, even when the FIFO is full, because it needs no new place. - An
UPDATEfor a symbol that is not pending, arriving while the FIFO is full, is handled bypolicy(table below). - Under either policy one quote is lost, and the dropped count grows by 1.
- An evicted quote counts as one drop even if it had absorbed several updates, because those updates were already counted as conflated.
- A symbol whose update was dropped, or that was evicted, is not pending, so its next
UPDATEis treated as new.
policy | Effect of an update for a new symbol when the FIFO is full |
|---|---|
DROP_NEW | Discard the update. The FIFO and every stored quote stay as they were. |
EVICT_OLDEST | Remove 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.
POLL_ALLdelivers every pending symbol and leaves the queue empty. With nothing pending it printsBATCH 0and nothing else.- As with
POLL, delivered symbols are no longer pending, so a laterUPDATEfor any of them appends instead of conflating.
Output
BATCH n symbol bid ask
n= the number of pending symbols.- Then
nlines in thePOLLformat, one per symbol in FIFO order, oldest first.
Tests 9–10 cover this part.
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
PENDING 2 AAPL 19012 19014 MSFT 41251 41253 AAPL 19013 19015 EMPTY PENDING 0 CONFLATED 3
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.