Hard
In a real-time data visualization dashboard, you need to implement a feature that tracks the average response time of API calls. The system should maintain a rolling window of the last 5 minutes of response times. What would be the output of the performance monitor below?
1class APIPerformanceMonitor {2 constructor(windowSize = 300000) { // 5 minutes in milliseconds3 this.measurements = new Map();4 this.windowSize = windowSize;5 }67 recordResponse(timestamp, duration) {8 this.measurements.set(timestamp, duration);9 this.cleanup(timestamp);10 }1112 cleanup(currentTime) {13 const threshold = currentTime - this.windowSize;14 for (let [timestamp] of this.measurements) {15 if (timestamp < threshold) {16 this.measurements.delete(timestamp);17 }18 }19 }2021 calculateAverageResponse(currentTime) {22 this.cleanup(currentTime);23 if (this.measurements.size === 0) return 0;2425 const sum = Array.from(this.measurements.values())26 .reduce((acc, val) => acc + val, 0);27 return Math.round(sum / this.measurements.size);28 }29}3031const monitor = new APIPerformanceMonitor();32const baseTime = Date.now();3334monitor.recordResponse(baseTime - 360000, 150); // 6 minutes ago35monitor.recordResponse(baseTime - 240000, 200); // 4 minutes ago36monitor.recordResponse(baseTime - 120000, 300); // 2 minutes ago37monitor.recordResponse(baseTime - 60000, 250); // 1 minute ago38monitor.recordResponse(baseTime, 100); // now3940console.log(monitor.calculateAverageResponse(baseTime));