1. Concept library
  2. Data Structures & Algorithms
  3. Module 2: Arrays and strings

Concept

Sliding window

A window of elements slides along an array, growing on the right and shrinking on the left, keeping a running summary. It finds the best contiguous span in O(n) rather than checking every start and end.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how a window grows and shrinks while keeping a running summary
  2. Solve a longest-substring problem with a sliding window
  3. Spot the recomputation that throws away the O(n) gain

In practice

A few lines that show Sliding window doing its job.

Python
def longest_unique_substring(s):
    last = {}
    start = best = 0
    for end, ch in enumerate(s):
        if ch in last and last[ch] >= start:
            start = last[ch] + 1
        last[ch] = end
        best = max(best, end - start + 1)
    return best

longest_unique_substring("abcabcbb")   # 3

The common mistake

Watch for

Recomputing the window's summary from scratch each time it moves. The whole gain comes from updating the count or sum incrementally as one element enters and one leaves.

What to know first

Your roadmap puts these before Sliding window, unless you already know them.

How it stays learned

Once a session shows you understand Sliding window, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Sliding window.

Previous in the module← Two pointers
Next in the modulePrefix sums →