Queue Mean Queries
Updated
Problem statement
Process a sequence of operations on an initially empty first-in, first-out queue of integers.
PUSH xappends integerxto the back of the queue.POPremoves the oldest value from the front. EveryPOPoccurs when the queue is nonempty.MEANrequests the arithmetic mean of the values currently in the queue. Its result is0.0when the queue is empty.
Return one floating-point result for each MEAN, in request order. PUSH and POP produce no result entries. A mean is the current sum divided by the current number of values; it must not use integer division.
Support each operation in constant amortized time. Returned values are compared with relative-or-absolute tolerance 10^-9.
Function
queueMeans(operations: String[]) → double[]Examples
Example 1
operations = ["MEAN","PUSH 5","MEAN","PUSH 9","MEAN","POP","MEAN","POP","MEAN"]return = [0.0,5.0,7.0,9.0,0.0]The requested queue states are [], [5], [5,9], [9], and []. Their means are 0, 5, 7, 9, and 0.
Example 2
operations = ["PUSH -2","PUSH 3","PUSH 3","MEAN","POP","MEAN","PUSH 0","MEAN"]return = [1.3333333333333333,3.0,2.0]The first mean is (-2 + 3 + 3) / 3 = 4/3. Removing the oldest value leaves [3,3]. Appending 0 leaves [3,3,0].
Example 3
operations = ["PUSH 1","POP"]return = []No MEAN request occurs, so the returned array is empty.
Constraints
1 <= operations.length <= 2 * 10^5.- Each command is exactly
POP,MEAN, orPUSH xwith a signed decimal integerx. -10^9 <= x <= 10^9.- Every
POPremoves a value from a nonempty queue. - All commands are valid. Use a signed
64-bit running sum.
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 from Corrective SnapshotsPHONE 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
- Moving Average of a Data StreamSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Smart Queue of Latest PricesCitadel · PHONE SCREENCitadel interview questions
- Implement a Queue Using Two StacksOracle · PHONE SCREENOracle interview questions
- Three Calls Within Three SecondsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Build a Queue From Two StacksAdobe · PHONE SCREENAdobe interview questions
- Rate Limiter with a Sliding Window per ClientStackAdapt · PHONE SCREENStackAdapt interview questions
- Rolling Hit CounterDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN