Sliding Window Average After Trimming Both Ends
Updated
Problem statement
You are given an integer array values, a window length windowSize, and an integer trimCount. Process the values in their given order.
For every full window of the latest windowSize elements, remove its trimCount smallest elements and its trimCount largest elements. Count equal values as separate occurrences. Return the arithmetic mean of the remaining elements, in left-to-right window order.
When trimCount == 0, return the ordinary average of each full window. Produce no result before a full window exists. Do not round or floor the averages. Answers use a relative or absolute tolerance of 10^-9.
Function
trimmedAverages(values: int[], windowSize: int, trimCount: int) → double[]Examples
Example 1
values = [3,1,10,5,5]windowSize = 3trimCount = 1return = [3,5,5]The windows sort to [1,3,10], [1,5,10] and [5,5,10]. Removing one occurrence from each end leaves 3, 5 and 5.
Example 2
values = [1,2,4,8]windowSize = 2trimCount = 0return = [1.5,3,6]No elements are removed. The full windows have means (1+2)/2 = 1.5, (2+4)/2 = 3 and (4+8)/2 = 6.
Example 3
values = [1,1,1,10,10,10]windowSize = 6trimCount = 2return = [5.5]Remove two of the three occurrences of 1 and two of the three occurrences of 10. The remaining values are [1,10], with mean 11/2 = 5.5.
Constraints
1 <= values.length <= 100000.0 <= values[i] <= 100000.1 <= windowSize <= values.length.0 <= trimCount <= 49999.2 * trimCount < windowSize.
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Maximize Element FrequencyGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Tagged Metric Window SumsDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Minimum Operations to Make an Array ContinuousAmazon · ONSITE INTERVIEW
- Closest Pair Among the Latest K ReadingsRippling · ONSITE INTERVIEWRippling interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Integer Compression OffsetHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Earliest Hour with Frequent Badge AccessIntuit · OAIntuit interview questions
- Sliding Window MedianAuxoAI · OA
- Detect Security Attacks from Network LogsFireworks AI · PHONE SCREEN