Temperature Monitor
Asked at Google
Reported by the problem's creator, not verified
A weather station reports integer temperature readings one at a time. A monitor keeps track of the n most recent readings, called the window, so its statistics can be read at any moment. The window counts readings, not time: it always holds the last n readings that were inserted, equal values included. Until n readings have arrived, the window holds all readings so far.
Examples
Example 1
Input: init =
[3], operations =[["insert",[10]],["moving_average",[]],["max_temp",[]],["insert",[20]],["moving_average",[]],["max_temp",[]],["insert",[15]],["moving_average",[]],["max_temp",[]],["insert",[5]],["moving_average",[]],["max_temp",[]],["insert",[1]],["moving_average",[]],["max_temp",[]]]Output:
[null,10,10,null,15,20,null,15,20,null,13.333333,20,null,7,15]Explanation: After 10, 20 and 15 the window is full: the average is 15 and the maximum is 20. Inserting 5 drops 10, so the window is 20, 15, 5 (average 13.333333, maximum 20). Inserting 1 drops 20, so the window is 15, 5, 1 (average 7, maximum 15).