A case study
The Best Stretch
Mile 1 · A Familiar Problem

You've seen this before

A cricket coach scans the scorecard for the best five overs in a row: the stretch where the team scored the most.

A fitness app lists your steps for every day this month. You want your most active seven days in a row.

Notice the shape: it isn't about one item, and not about everything, just a stretch of consecutive items, always the same length. And the best stretch isn't always where the biggest single number sits.

Next stop, you'll hunt for the best stretch yourself.

Mile 2 · Before You Start

How this stop works

You're a sales analyst at a small shop. Each challenge shows daily sales (₹ in thousands) and a window size, like 3 days. Find the most profitable stretch of that many consecutive days.

14229311218

A 3-day window: 22 + 9 + 31 = ₹62k. Drag it along the days, watch the total change, then submit your best stretch.

Your task: play each of the 3 challenges yourself with Try it yourself, or press Automatic to watch it solve.

Mile 3 · The Sales Floor

Mile 4 · Debrief

What just happened?

Think back on the sales floor. Talk these through. No answer to click.

1

Where do you think this kind of window would be useful in real life? Can you think of situations where looking at a few consecutive days together is more useful than looking at one day alone?

2

Why do you think the size of the window matters? If you could choose any size instead of just 3, 4, or 5 days, what would influence your choice?

3

Suppose two windows have the same total sales, but one has a single very high-sales day while the other has more balanced sales. Which one would you choose, and how would you decide?

4

What could go wrong if you accidentally skipped a day while moving the window? How might that affect the stretch you identify as the best?

5

As you move the window one day at a time, what advantage do you get from using the previous window's total instead of calculating the new total from scratch?

Mile 5 · The Concept

The formal picture

What you did has a name: the sliding window: keep a fixed-size frame over consecutive items, and update it by dropping one and adding one instead of recomputing everything.

Windowk consecutive items
Slidemove one step
Updatedrop one, add one
Track bestkeep the max so far
sum(i) = sum(i−1) − a[i−1] + a[i+k−1]
Time: O(n·k) → O(n)
Without: re-add all k days for every window
With: drop one, add one
Mile 6 · Checkpoint

Spot the pattern elsewhere

Same shape, different scenery. Talk through each one.

1
Finding the Minimum

A temperature-monitoring system records these temperatures over 7 days: 31, 28, 30, 26, 29, 25, 32

2

A stock market analytics platform receives thousands of price updates every minute. It needs to detect the period with the highest average price over a fixed number of consecutive updates without recalculating every average from scratch. Design an approach that processes incoming data efficiently while keeping the result up to date.

3

A fitness application tracks daily steps for millions of users and wants to identify each user's most active 7-day period. Some users have months of historical data, while others have years. Design an approach that finds the best period efficiently and explain how it can handle datasets of different sizes.

4

A network monitoring system records the number of requests received by a server every second. It must detect the highest traffic volume within any 60-second period, even when traffic suddenly spikes. Design a solution that updates the window efficiently and identifies the busiest period.

5

A hospital monitors the number of emergency admissions every hour and wants to identify the consecutive 24-hour period with the highest number of admissions. The system must update its results as new hourly data arrives. Design an approach that avoids repeatedly processing all 24 hours and explain how it handles the arrival of new data.

6

A delivery company wants to find the shortest consecutive period in which it completes at least 100 deliveries. The number of deliveries each hour is non-negative. Why might a window that expands and shrinks be more suitable than a fixed-size window?

7

A weather monitoring system records hourly temperatures and needs to identify the consecutive 6-hour period with the lowest average temperature. The system receives new readings continuously and must update its result without repeatedly calculating every temperature average. Design an efficient solution and explain how it determines the coldest period.

8

A manufacturing plant records the number of defective products produced each hour. The quality team wants to find the shortest consecutive period in which the total number of defects reaches a specified threshold. Since the required period can vary in length, design an approach that adapts the window as new values are examined and explain how it differs from using a fixed-size window.

Same shape, different costumes, every time.

Heads up

Going back will erase your progress on this stop. Continue?