Learner Level
Implement the movingAverageStreamLast method that returns the last moving average value for a fixed-size window.
The input contains an integer array nums, its length size, and a window size window. The values arrive like a stream from left to right.
After each new value, the moving average is calculated using at most the last window values. Your task is to return the final moving average after all values are processed.
The returned value should be the integer floor of the final average.
Use a queue or sliding window sum.
Add each new value to the window and update the running sum. If the window contains more than the allowed number of values, remove the oldest value and subtract it from the sum.
After all values are processed, divide the current sum by the number of values currently in the window and return the integer result.
Pseudocode:
function movingAverageStreamLast(nums, size, window):
queue = empty queue
sum = 0
for each value in nums:
push value into queue
sum = sum + value
if size of queue > window:
removed = pop front from queue
sum = sum - removed
if queue is empty:
return 0
return floor(sum / size of queue)