FastPrepQueue Mean from Corrective Snapshots
Problems/Queue Mean from Corrective Snapshots

Queue Mean from Corrective Snapshots

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Headlands Technologies phone screen. The report describes a queue with event IDs that have gaps. Snapshots are trusted. You find the mean of the part of the queue you can rebuild. The report has an example and a hint about using two queues. It does not say how to handle duplicates, old snapshots, popping an empty queue, the function signature or the limits. We filled these in. Our version is about 85% the same as the reported question.

A FIFO queue starts empty. Ordinary events (PUSH and POP) belong to one history and have consecutive positive IDs starting at 1. Some of these events never arrive. You also receive snapshots that show the exact queue contents at some point, so they can fill those gaps.

Process the integer-array commands in the order they arrive:

  • [0, eventId, value]: a PUSH event that adds value to the back of the queue.
  • [1, eventId]: a POP event that removes the oldest value. A POP on an empty queue does nothing.
  • [2, snapshotId, v1, v2, ...]: a snapshot. Its values are the complete queue after all events up to and including event snapshotId. A snapshot with no values means an empty queue.
  • [3]: report the mean of the values in the committed queue.

The committed frontier is the highest event ID whose effect on the queue is known. It starts at 0. Apply ordinary events only in consecutive ID order after the frontier. If an event arrives after a missing ID, keep it in a buffer until the gap is filled.

A snapshot newer than the committed frontier replaces the queue with its values. Move the frontier up to the snapshot ID, throw away buffered events at or below it, and then apply any buffered events that now follow in consecutive order. Ignore any snapshot or ordinary event at or below the committed frontier.

Event IDs of PUSH and POP commands never go down. If two have the same ID, the second is an identical resend and you ignore it. Different IDs go up but may skip numbers. Snapshots can arrive in any order, even with an ID lower than the latest event you received. All events and snapshots come from one consistent history. Inputs that conflict are not given.

Return one double for each mean command, in order. Events still waiting behind a gap do not count. The mean of an empty queue is 0. If there are no mean commands, return an empty array. Keep the sum in a signed 64-bit integer so it stays exact, then divide 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]

Events 1 and 2 push 2 and 3. The POP with ID 4 arrives, but event 3 is missing, so it waits in the buffer. The snapshot with ID 3 says the queue is [3], so the frontier moves to 3 and the buffered POP runs. The queue is now empty, so the mean is 0.

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, event 3 is buffered behind missing event 2, so MEAN sees only [10]. Snapshot 2 supplies [10,20], then event 3 produces [10,20,30]. Event 4 removes 10, 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 event 1 is ignored. Snapshot 1 is stale after events 1 and 2 are committed. Snapshot 5 replaces the queue with authoritative [6,1,2]; the later ordinary event 4 is already covered and ignored.

Constraints

  • 0 <= commands.length <= 100000.
  • Commands have exactly the shapes described above, with event and snapshot IDs from 1 through 1000000000.
  • Every pushed or snapshotted value is an integer from -1000000000 through 1000000000.
  • 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.

More Headlands Technologies Phone Screen questions

  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Market Order BookPHONE SCREEN · Seen Jun 2026
  • Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
  • Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
  • Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
  • CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
  • Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
  • Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
  • Fixed-Size Memory PoolPHONE SCREEN · Seen May 2024

Similar questions at other companies

  • Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
  • VIP and Regular Service SchedulerIntuit · PHONE SCREENIntuit interview questions
  • Callback Signal RegistryCandid Health · PHONE SCREEN
  • Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
  • Sliding-Window API Rate LimiterPatreon · PHONE SCREEN
  • High-Card Pot GameAffirm · PHONE SCREENAffirm interview questions
  • Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
  • Batch API Requests with Retry DelaysDecagon · PHONE SCREENDecagon interview questions
  • Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
  • Rate Limiter Sliding Window With Per-Entity LimitsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public double[] snapshotQueueMeans(int[][] commands) {
        // write your code here
    }
}
Checking account…