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

Ring Buffer Backpressure

Prerequisites: SPSC Ring Buffer Handoff, Condition Variables and Producer-Consumer Queues

The feed thread pushes AAPL market updates into a fixed-size buffer, and the strategy thread pops them. During a burst the feed pushes faster than the strategy pops, and the buffer fills. Simulate what the buffer does when a push finds it full.

  • Overflow policy: a fixed rule for which value loses when a push has nowhere to store its value. The buffer can keep the queued values and discard the new one, keep the new one and discard the oldest, or refuse the push and tell the producer.
  • Backpressure: the last choice. The full buffer pushes back on the producer instead of losing a value without a trace.

The simulation is deterministic and single-threaded. Pushes and pops arrive in one command stream and are applied one at a time, in order. It models the decision a full buffer makes, not the synchronization between producer and consumer.

What to implement

  • process_commands(capacity, policy, commands) returns the printed lines, in command order. Each command is one line.
  • The buffer holds at most capacity integers (one per update) in first-in, first-out (FIFO) order. It starts empty.
  • policy is the overflow policy, fixed for the whole run.
  • On stdin: the capacity C, then the policy, then the number of commands Q, then the Q commands, one per line.
  • There are three parts, and each adds a policy. One solution must pass all ten tests.
CommandEffectPrints
PUSH xAppend the integer x as the newest value; if the buffer is full, apply the policynothing, except BLOCKED under the BLOCK policy (Part 3)
POPRemove the oldest valuethat value, or EMPTY if the buffer is empty
PEEKRead the oldest value without removing itthat value, or EMPTY if the buffer is empty

Part 1: Drop the new value

  1. The policy is DROP_NEW.
  2. A push into a full buffer is discarded. The buffer keeps the values it holds, and the push prints nothing.
  3. Once a POP frees a slot, the next PUSH succeeds as usual, so a later command can push a discarded value again.

Limits

  • C is between 1 and 105. Q is between 1 and 2 · 105.
  • Every pushed value fits in a signed 32-bit integer.

Tests 1–6 cover this part. All of them use DROP_NEW.

Part 2: Prefer fresh data

Add the DROP_OLD policy. DROP_NEW must keep working.

  1. A push into a full buffer removes the oldest value, then appends the new one. The buffer keeps the most recent C values.
  2. The push still prints nothing.

This suits data whose newest value matters most, such as the latest AAPL price: a strategy that has fallen behind would rather see the newest updates than stale ones.

Tests 7–8 cover this part.

Part 3: Signal backpressure

Add the BLOCK policy. POP, PEEK, and the other two policies keep their behavior.

  1. A push into a full buffer leaves the buffer unchanged and prints BLOCKED, telling the producer the value was not stored.
  2. There is no automatic retry. A later PUSH of the same value is a separate attempt, and succeeds only if a POP has freed a slot in the meantime.
  3. Only BLOCK makes a PUSH print anything, and only when the buffer is full.

In a threaded program, this is the policy where the producer waits:

  • with a condition variable, it would sleep until the consumer frees a slot;
  • with the SPSC ring, whose push returns false when the ring is full, it would try again.

The simulation prints BLOCKED and leaves any retry to a later command.

Tests 9–10 cover this part.

Example
Input
2
DROP_NEW
9
PUSH 5
PUSH 6
PUSH 7
POP
PUSH 8
PEEK
POP
POP
POP
Output
5
6
6
8
EMPTY
Explanation

The buffer holds two values. PUSH 5 and PUSH 6 fill it, so PUSH 7 is discarded and prints nothing.

POP prints the oldest value, 5, which frees a slot, and PUSH 8 takes it. The buffer now holds 6 and then 8.

PEEK prints 6 and leaves it in place. The next two POP commands print 6 and 8, and the last POP finds the buffer empty.