Queue Mean from Corrective Snapshots
Problem statement
A FIFO queue starts empty. Ordinary events belong to one logical history with consecutive positive IDs starting at 1. Some events are missing from the received stream. You also receive authoritative snapshots that can cover those gaps.
Process the integer-array commands in arrival order:
[0, eventId, value]: receive a PUSH event that appendsvalue.[1, eventId]: receive a POP event that removes the oldest value. POP on an empty queue does nothing.[2, snapshotId, v1, v2, ...]: receive a snapshot whose remaining values are the complete FIFO state after all logical events throughsnapshotId, including that event. A snapshot with no remaining values represents an empty queue.[3]: report the mean of the currently committed queue.
The committed frontier is the largest logical ID through which the queue can be reconstructed. Initially it is 0. Apply ordinary events only when their IDs extend the committed frontier consecutively; buffer events beyond a gap.
For a snapshot newer than the committed frontier, replace the queue with its authoritative values, advance the frontier to the snapshot ID, discard buffered events at or below it, and apply any buffered consecutive events that follow. Ignore a snapshot or ordinary event at or below the committed frontier.
Across ordinary PUSH and POP commands, event IDs are nondecreasing. Equal IDs are identical retransmissions and are ignored; distinct IDs increase but may have gaps. Snapshot IDs may arrive in any order, including below the latest received ordinary event ID. All copies and snapshots are consistent with one logical history; conflicting inputs are excluded.
Return one double for each MEAN command, in order. Pending events beyond a gap do not contribute. The mean of an empty queue is 0. If there are no MEAN commands, return an empty array. Maintain an exact signed 64-bit sum before dividing by the queue size.
Function
snapshotQueueMeans(commands: int[][]) → double[]Examples
Example 1
commands = [[0,1,2],[0,2,3],[1,4],[2,3,3],[3]]return = [0.0]The original worked sequence: event3 POP is missing, but the snapshot at3 contains [3]. It covers the gap and allows buffered event4 POP to run. The resulting queue is empty, so MEAN returns0.
Example 2
commands = [[0,1,10],[0,3,30],[3],[2,2,10,20],[3],[1,4],[3]]return = [10.0,20.0,25.0]Before the snapshot, event3 is buffered behind missing event2, so MEAN sees only [10]. Snapshot2 supplies [10,20], then event3 produces [10,20,30]. Event4 removes10, leaving [20,30].
Example 3
commands = [[0,1,-2],[0,1,-2],[0,2,6],[3],[2,1,-2],[3],[1,3],[3],[2,5,6,1,2],[0,4,1],[3]]return = [2.0,2.0,6.0,3.0]The identical event1 is ignored. Snapshot1 is stale after events1 and2 are committed. Snapshot5 replaces the queue with authoritative [6,1,2]; the later ordinary event4 is already covered and ignored.
Constraints
0 <= commands.length <= 100000.- Commands have exactly the shapes described above, with event and snapshot IDs from
1through1000000000. - Every pushed or snapshotted value is an integer from
-1000000000through1000000000. - The total number of pushed values and values supplied inside all snapshots is at most
200000. - All ordinary-event copies and snapshots are consistent with one logical FIFO history.