Ok Garmin, save video
Implement Replayer, a component that replays events in sequence order even when they arrive out of order.
This models a common infrastructure pattern: buffering, reordering, and bounded memory usage.
Event Model
Each event has:
seq: a sequence numberpayload: an opaque value you should replay in order
Constructor
Replayer(start_seq, max_buffer)
- Initializes the replayer.
- The next expected sequence number is
start_seq. max_bufferis the maximum number of distinct out-of-order sequence numbers that may be buffered at once.
Methods
push(seq, payload)
- Adds an event
(seq, payload)to the replayer. - Returns any payloads that became replayable as a result of this call, in increasing sequence order starting from the current expected sequence.
- If the event’s
seqis a duplicate of one already seen (buffered or already replayed), it must be ignored. - If buffering this event would cause the number of buffered distinct sequence numbers to exceed
max_buffer, you must signal failure by printingerror.
Replay Rules
- Payloads may only be returned when the replayer has a contiguous run beginning at the expected sequence. If it is not possible, print
null. - After replaying sequence
s, the next expected sequence becomess + 1.
Notes
- This is an incremental component, not a batch sorting task.
- Correct handling of duplicates, gaps, and bounded buffering is required.
Examples
Example 1
Input:
Replayer 5 3
push 6 b
push 8 d
push 5 a
push 7 cOutput:
null
null
a b
c dAccepted 2/3
Acceptance 67%
Loading editor...
Sample Input:
Replayer 5 3
push 6 b
push 8 d
push 5 a
push 7 cExpected Output:
null
null
a b
c d