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

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 AAPL and 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, written W below, 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.
CommandMeaningPrints
EVENT id key deltaAdd delta to the running sum for keyAPPLIED sum with the key's new sum, or DUPLICATE (Part 2)
QUERY keyRead the running sum for keythe sum, or 0 for a key no event has changed

Part 1: Running sums

  1. Every EVENT id key delta adds delta to the sum for key and prints APPLIED s, where s is that key's sum after the addition.
  2. A delta of zero still counts as applied and still prints.
  3. QUERY key prints the key's current sum. A key that no event has changed reads as 0.
  4. Event ids are distinct in this part, so no id affects a result yet and W is not needed.

Limits

  • Keys are non-empty, case-sensitive strings without whitespace, so A and a are different keys.
  • At most 2 × 105 commands. W is 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.

  1. When an EVENT's id is in the window, print DUPLICATE and change nothing. The key's sum stays the same, and so does the window.
  2. A duplicate is not a new arrival, so its id keeps its old place instead of moving to the newest position.
  3. 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.
  4. If the window then holds W + 1 ids, its oldest id leaves.
  5. 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.
  6. QUERY never 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.

Example
Input
3
6
EVENT 1 x 9
EVENT 2 y -4
QUERY x
EVENT 3 x -10
QUERY x
QUERY y
Output
APPLIED 9
APPLIED -4
9
APPLIED -1
-1
-4
Explanation

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.