Given a stream of real-time sensor data, write a memory-efficient sliding window algorithm to calculate moving averages and detect anomalies.