Question: Suppose there is a real time feed of stock prices, how do you calculate the average of a subset of it (say over the past week)? 

This was an interview question. I can come up with an algorithm to do it in O(n^2), but the interviewer wanted an algorithm that was O(n).

Answer: 
A useful approach is to compute the cumulative sum of your array.

This means that each entry in the cumulative sum array is the sum of all previous prices.

This is useful because you can then generate the sum over any particular subarray of your input using a single subtraction.

Note that when a new input arrives, you only need 1 addition to compute the new cumulative sum (because you simply add the new element to the old cumulative sum).
Another approach is akin to computing skew in Genomics. 

If you want to compute the average over the past week, create a variable that contains the sum over a moving window. When an entry is created, add the entry to the above sum variable, and subtract the oldest entry in the moving window from it. Since the size of the window is constant, the average over the past week is just the moving sum over the number of entries in the past week.