Sliding-Window Averages as Reduced Fractions
Updated
Problem statement
You are given a positive integer windowSize and an array values. Process the values from left to right as successive calls to a sliding-window statistic.
After each value arrives, compute the average of the most recent windowSize values, or of all values seen so far when fewer have arrived. Return one pair [numerator, denominator] per arrival. Every fraction must be reduced to lowest terms, and its denominator must be positive.
The result therefore preserves every state transition of the ordered stream without floating-point rounding.
Function
slidingWindowAverages(windowSize: int, values: int[]) → long[][]Examples
Example 1
windowSize = 3values = [1,10,3,5]return = [[1,1],[11,2],[14,3],[6,1]]The active windows are [1], [1,10], [1,10,3], and [10,3,5]. Their reduced averages are 1, 11/2, 14/3, and 6.
Example 2
windowSize = 2values = [-1,2,-3]return = [[-1,1],[1,2],[-1,2]]The final active window is [2,-3], whose average is -1/2.
Example 3
windowSize = 5values = [0,0]return = [[0,1],[0,1]]Zero averages are normalized to 0/1.
Constraints
1 <= windowSize <= 100000.0 <= values.length <= 100000.-1000000000 <= values[i] <= 1000000000.- Process values in their supplied order.
- Use signed 64-bit arithmetic for the running sum and returned fraction components.
More Coinbase interview questions
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Parse and Order Structured LogsONSITE INTERVIEW · Seen Aug 2026
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Banking System with Historical SnapshotsOA · Seen Aug 2026
- Find the Length of the Longest Common PrefixOA · Seen Oct 2024
- Lexicographically Smallest Topological OrderPHONE SCREEN · Seen Apr 2022
Similar questions at other companies
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
- Reservoir Sampling with Recorded DrawsMeta · ONSITE INTERVIEWMeta interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Apply a Sequence of Array RotationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Generate Unique Random NumbersGoogle · ONSITE INTERVIEWGoogle interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions