Real-Time Temperature Window Statistics
Problem statement
Process a finite sequence of operations for a real-time temperature stream with a fixed positive windowSize. Each operation is one of:
ADD <temperature>: append one integer reading.AVERAGE: report the exact average of the latestwindowSizereadings, or all readings so far when fewer exist.MAX: report the maximum over that same current window.
If a query occurs before any reading has been added, return "null". Otherwise, return an average as a reduced numerator/denominator string with a positive denominator, and return a maximum as its decimal integer string. Normalize a zero average to 0/1.
Examples
Example 1
operations = ["AVERAGE","MAX","ADD 5","AVERAGE","MAX","ADD 1","ADD 9","AVERAGE","MAX","ADD 3","AVERAGE","MAX"]windowSize = 3return = ["null","null","5/1","5","5/1","9","13/3","9"]The first two queries have no readings. After adding 5, both statistics use [5]. After 5, 1, 9, the average is 5/1 and the maximum is 9. Adding 3 evicts 5, leaving [1,9,3].
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.