AlgoMaster Logo
AlgoMasterSimulate Log Pipeline Backpressuremedium

Simulate Log Pipeline Backpressure

medium

When a log backend slows down, agents need a bounded buffer and an explicit overflow policy. An unlimited queue can fill the host disk and turn an observability failure into an application outage.

Design a LogBufferSimulator class:

  • LogBufferSimulator() creates a stateless simulator with an empty initial buffer.
  • int[] bufferLevels(...) returns the buffered count after each interval finishes.
  • int[] droppedPerInterval(...) returns how many arrivals were rejected in each interval.

For interval i, perform operations in this exact order:

  1. arrivals[i] events try to enter the buffer.
  2. Accept only events that fit within capacity; drop the rest.
  3. Drain up to drainCapacities[i] buffered events.
  4. Record the remaining buffer and this interval's dropped count.

Drain capacity does not carry into later intervals. Each method call starts with an empty buffer.

Example 1:

Input:

Output:

Explanation: The first interval buffers 5 and drains 3. The next accepts all 8 because 8 slots are free, then drains 3. The final drain clears the remaining 9 events.

Example 2:

Input:

Output:

Explanation: Backlog consumes buffer space. Only four arrivals fit in interval 2 and two fit in interval 3.

Constraints

  • 1 <= arrivals.length == drainCapacities.length <= 10^5
  • 0 <= arrivals[i], drainCapacities[i], capacity <= 10^9
  • Buffer calculations fit in a signed 32-bit integer.
  • Arrivals are admitted before draining in each interval.
  • At most 100 total method calls are made.
Hints

Loading...
CallReturns
new LogBufferSimulator()null
bufferLevels([5,8,2], [3,3,10], 10)[2,7,0]
droppedPerInterval([5,8,2], [3,3,10], 10)[0,0,0]

The buffer grows to 2 and then 7 before the final drain clears it. Every arrival fits when it occurs.

Run checks these cases. Submit also runs a larger hidden set.