Deduplicated Stream Sums
Prerequisites: Market Data Feed Handler Shape, TCP vs UDP Trade-Offs
A consumer downstream of the feed handler keeps running totals from a stream of events. Implement that consumer.
- Event: an id, a key, and a signed change called a delta. The key might be a symbol such as
AAPLand the delta a change in quantity, but the consumer treats both as opaque values.
What to implement
process_commands(window_size, commands)returns one output line per command, in order. Each command is one line.window_size, writtenWbelow, is loaded once before the stream starts.- On stdin, the first line holds
W, the second holds the number of commands, and the commands follow one per line. - There are two parts. Part 2 adds behavior on top of Part 1. One solution must pass all eleven tests.
| Command | Meaning | Prints |
|---|---|---|
EVENT id key delta | Add delta to the running sum for key | APPLIED sum with the key's new sum, or DUPLICATE (Part 2) |
QUERY key | Read the running sum for key | the sum, or 0 for a key no event has changed |
Part 1: Running sums
- Every
EVENT id key deltaaddsdeltato the sum forkeyand printsAPPLIED s, wheresis that key's sum after the addition. - A delta of zero still counts as applied and still prints.
QUERY keyprints the key's current sum. A key that no event has changed reads as0.- Event ids are distinct in this part, so no id affects a result yet and
Wis not needed.
Limits
- Keys are non-empty, case-sensitive strings without whitespace, so
Aandaare different keys. - At most 2 × 105 commands.
Wis between 1 and 2 × 105. - Deltas, event ids and running sums can all need the full signed 64-bit range, so store them in
std::int64_t.
Tests 1–6 cover this part.
Part 2: Deduplicate recent events
The same event can now arrive more than once, and applying a copy would add its delta a second time. Copies come from:
- UDP, which may, rarely, duplicate a datagram.
- A sender that resends an event when its acknowledgment does not come back in time, the way TCP retransmits. If the first copy arrived and only the acknowledgment was slow or lost, the resend is a second copy.
- The A and B feeds: exchanges commonly publish the same market-data feed on two independent multicast lines, so a packet dropped on one usually still arrives on the other. A consumer reading both lines receives most messages twice.
Remembering every id ever applied would let that memory grow for as long as the stream runs. So the consumer remembers only a window: the most recent W distinct ids it has applied.
- When an
EVENT's id is in the window, printDUPLICATEand change nothing. The key's sum stays the same, and so does the window. - A duplicate is not a new arrival, so its id keeps its old place instead of moving to the newest position.
- When the id is not in the window, apply the event as in Part 1 and add its id to the window as the newest.
- If the window then holds
W + 1ids, its oldest id leaves. - An id that has left the window is new again: a later event with that id is applied, and it may carry any key and any delta.
QUERYnever changes the window.
Tests 7–11 cover this part. Test 10 uses ids, deltas and sums beyond the 32-bit range and brings an evicted id back under a different key.
3 6 EVENT 1 x 9 EVENT 2 y -4 QUERY x EVENT 3 x -10 QUERY x QUERY y
APPLIED 9 APPLIED -4 9 APPLIED -1 -1 -4
The first line is the window size, 3, and the second says six commands follow. Keys x and y keep separate sums.
Event 1 brings x to 9 and event 2 brings y to -4, so the first query prints 9. Event 3 adds -10 to x, which leaves x at -1 while y stays at -4.
Every id is new, so each event prints APPLIED with its key's new sum.