Peak Hour Analysis

Sliding Window Technique

Peak Traffic Analysis

Identify peak traffic times on a web appliation.

Every website owner or administrator wants to understand when their site experiences peak traffic. This information is valuable for scheduling maintenance, running promotions, or simply understanding user behavior.

Task

Given a time-series dataset of hourly website traffic, your task is to identify the 3-hour window with the highest average traffic.

Input

The input is an array of integers, where each integer represents the number of visitors in a given hour. The array's index corresponds to the hour (e.g., index 0 corresponds to the first hour, index 1 to the second hour, and so on).

Output

Your function should return the starting hour of the 3-hour window with the highest average traffic. If there is more than one window with the same average traffic, return the earliest one.

Given a time-series dataset of hourly website traffic, create a function to return the start of the 3 hour window with the highest average traffic.

In this function, we begin by calculating the sum of visitors in the first 3-hour window and calculating the average. As we slide our window across the data, we update window_sum and window_avg for each new position. If we encounter a higher average traffic than our current maximum, we update max_avg and max_hour.

This function will provide the starting hour of the 3-hour window with the highest average traffic, allowing website administrators to understand their peak usage times better.

Video Walkthrough

Up Next

Two Pointers Technique