Moving Average of a Data Stream
Updated
Problem statement
Integers arrive one at a time in a stream. You are given a window size size and the whole stream as the array values, in the order the numbers arrive.
Right after each number arrives, report the average of the last size numbers of the stream. If fewer than size numbers have arrived so far, report the average of all of them.
Return an array answer where answer[i] is the average reported right after values[i] arrives. An answer within 10^-5 of the correct value is accepted; for values larger than 1 in size, the allowed error is 10^-5 times the correct value.
Function
movingAverage(size: int, values: int[]) → double[]Examples
Example 1
size = 3values = [4,10,-2,7,1]return = [4.0,7.0,4.0,5.0,2.0]- After
4: the window is[4], average4.0. After10:[4, 10], average7.0. - After
-2:[4, 10, -2], average12 / 3 = 4.0. The window is now full. - After
7:4leaves, so the window is[10, -2, 7], average5.0. - After
1:10leaves, so the window is[-2, 7, 1], average2.0.
Example 2
size = 1values = [5,-3]return = [5.0,-3.0]With size = 1, each average is just the newest value.
Example 3
size = 5values = [1,1,2,2]return = [1.0,1.0,1.3333333333333333,1.5]Only four values arrive, fewer than size = 5, so each answer averages everything so far: 1 / 1, 2 / 2, 4 / 3 and 6 / 4.
Constraints
1 <= size <= 10001 <= values.length <= 1000-10^5 <= values[i] <= 10^5
More Squarepoint Capital Phone Screen questions
- Lazy Filter over a GeneratorPHONE SCREEN · Seen Nov 2026
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
- Probability of Consecutive HeadsPHONE SCREEN · Seen Nov 2026
- Three Points in a Semicircle: Monte Carlo EstimatePHONE SCREEN · Seen Nov 2026
- Combine Two Index SequencesPHONE SCREEN · Seen Aug 2026
- Parse, Normalize, and Combine PolynomialsPHONE SCREEN · Seen Aug 2026
- Sorted Insertion SnapshotsPHONE SCREEN · Seen Aug 2026
- Strong and Weak Reference LifecyclePHONE SCREEN · Seen Aug 2026
- Target Sum Across Two Sorted ArraysPHONE SCREEN · Seen Aug 2026
- Classify Heap Addresses from a Memory SnapshotPHONE SCREEN · Seen Nov 2025
Similar questions at other companies
- Three Calls Within Three SecondsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Sliding Window Average (Meta Canada, E4/E5 :)Meta · PHONE SCREEN · ONSITE INTERVIEWMeta 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
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Rolling CSAT TrackerDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Sliding-Window Rate LimiterUber · PHONE SCREENUber interview questions