Design a message buffer that batches incoming messages and flushes them when a count limit is reached or the oldest message has been held past a timeout on a logical clock. It tests the order of flush checks, inclusive threshold edge cases and the amortized cost of batching.
Design a `MessageBuffer` that batches incoming messages and flushes them based on two policies.
The buffer must flush all currently held messages when either of the following thresholds is met: the maximum message count is reached, or any message has been in the buffer for at least the maximum time allowed (the flush timeout).
Time is supplied externally via a logical clock. The buffer is constructed with `maxCount` and `maxTime`. Each `add` call provides the current time, and `tick` advances the logical clock to inspect timeouts.
Implement the following methods:
- `add(message: str, time: int) -> List[str]`: Add a message at the given time. If either the count or time threshold triggers a flush, return the flushed messages; otherwise return an empty list.
- `tick(time: int) -> List[str]`: Advance the logical clock to the given time. If the oldest buffered message has been held for at least `maxTime`, flush and return the messages; otherwise return an empty list.
- `flush() -> List[str]`: Forcefully flush and return all currently buffered messages in insertion order.
- `size() -> int`: Return the number of messages currently buffered.
A message added at time `t` has been held for `now - t` at time `now`. When `add(message, time)` is called and the messages already in the buffer have reached `maxTime` at `time`, those messages are flushed and returned, and the new message stays in the buffer as the first message of the next batch. Flushed messages are always returned in insertion order.
```hint What decides a timeout
Every flush empties the whole buffer. Work out which single message decides whether the time threshold has been met.
```
```hint Order of steps in add
A message can arrive after the buffered ones have already expired. Decide the order of the time check, the append and the count check, then test your order on Example 3.
```
### Examples
Example 1:
```text
Input:
["MessageBuffer", "add", "add", "add", "size", "flush"]
[[3, 10], ["a", 0], ["b", 1], ["c", 2], [], []]
Output: [null, [], [], ["a", "b", "c"], 0, []]
Explanation:
buf = MessageBuffer(3, 10) # maxCount=3, maxTime=10
buf.add("a", 0) # -> [] (buffer: [a])
buf.add("b", 1) # -> [] (buffer: [a, b])
buf.add("c", 2) # -> [a, b, c] (count reached 3, flush)
buf.size() # -> 0 (buffer empty)
buf.flush() # -> [] (nothing to flush)
```
Example 2:
```text
Input:
["MessageBuffer", "add", "add", "tick", "tick", "size"]
[[5, 10], ["x", 0], ["y", 3], [9], [10], []]
Output: [null, [], [], [], ["x", "y"], 0]
Explanation:
buf = MessageBuffer(5, 10) # maxCount=5, maxTime=10
buf.add("x", 0) # -> [] (buffer: [x@0])
buf.add("y", 3) # -> [] (buffer: [x@0, y@3])
buf.tick(9) # -> [] (oldest age = 9 < 10)
buf.tick(10) # -> [x, y] (oldest age = 10, flush)
buf.size() # -> 0
```
Example 3:
```text
Input:
["MessageBuffer", "add", "add", "size", "add", "size"]
[[2, 5], ["p", 0], ["q", 7], [], ["r", 8], []]
Output: [null, [], ["p"], 1, ["q", "r"], 0]
Explanation:
buf = MessageBuffer(2, 5) # maxCount=2, maxTime=5
buf.add("p", 0) # -> [] (buffer: [p@0])
buf.add("q", 7) # -> [p] (p has age 7 >= 5, flushed; buffer: [q@7])
buf.size() # -> 1
buf.add("r", 8) # -> [q, r] (q has age 1 < 5; r brings the count to 2, flush)
buf.size() # -> 0
```
### Constraints & Assumptions
- `1 <= maxCount <= 10⁴`
- `1 <= maxTime <= 10⁹`
- Time values passed to `add` and `tick` are non-decreasing.
- Messages are non-empty strings of length up to `100`.
### What a Strong Answer Covers
- The state the buffer keeps, and how cheaply each call can tell whether the time threshold has been met.
- The order of steps in `add`: expiring the current batch, appending, and the count check, and what `add` returns when a flush fires.
- Inclusive thresholds (`>=` for both count and age) and the behavior of `tick` and `flush` on an empty buffer.
- Time and space complexity, including the amortized cost of flushing.
- The limitation that time-based flushes only happen when `add` or `tick` is called.
### Follow-up Questions
- In production nobody calls `tick` for you. How would you flush on time with a real clock, and what concurrency problems does that introduce?
- How would you add a third policy, such as a maximum total size in bytes, without turning `add` into a tangle of conditions?
- A flush sends the batch to a downstream service that can fail or slow down. What happens to the batch, and how do you keep memory bounded?
- Several producer threads call `add` at once. How do you keep batches correct without holding a lock while the batch is being sent?
Overview: Design a message buffer that batches incoming messages and flushes them when a count limit is reached or the oldest message has been held past a timeout on a logical clock. It tests the order of flush checks, inclusive threshold edge cases and the amortized cost of batching.
Design a MessageBuffer that batches incoming messages and flushes them based on two policies.
The buffer must flush all currently held messages when either of the following thresholds is met: the maximum message count is reached, or any message has been in the buffer for at least the maximum time allowed (the flush timeout).
Time is supplied externally via a logical clock. The buffer is constructed with maxCount and maxTime. Each add call provides the current time, and tick advances the logical clock to inspect timeouts.
Implement the following methods:
add(message: str, time: int) -> List[str]
: Add a message at the given time. If either the count or time threshold triggers a flush, return the flushed messages; otherwise return an empty list.
tick(time: int) -> List[str]
: Advance the logical clock to the given time. If the oldest buffered message has been held for at least
maxTime
, flush and return the messages; otherwise return an empty list.
flush() -> List[str]
: Forcefully flush and return all currently buffered messages in insertion order.
size() -> int
: Return the number of messages currently buffered.
A message added at time t has been held for now - t at time now. When add(message, time) is called and the messages already in the buffer have reached maxTime at time, those messages are flushed and returned, and the new message stays in the buffer as the first message of the next batch. Flushed messages are always returned in insertion order.