This is straightforward math. If we have seen a total of n numbers so far, and the current average is avg, when a new element is added, the new average can be calculated as avg = (n * avg + new_element) / (n + 1). Below is the pseudocode to compute the moving average of all numbers in a stream in O(1) time: