DSA & Implementation · core

Sliding Window

Variable/fixed window over sequences.

dsasliding-window

Mental model

A sliding window maintains a contiguous range and an aggregate (sum, count, max) as it moves. Expand the right edge to include elements; shrink the left edge when a constraint breaks. Each element enters and leaves the window at most once — O(n).

How to study Sliding Window

Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use NeetCode Roadmap, Sliding Window — USACO Guide (Gold), Minimum Stack / Minimum Queue — the O(n) sliding-window minimum to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.

Next, compare Sliding Window with the neighboring concepts in its roadmap. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Longest substring without repeats, Longest Substring Without Repeating Characters and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.

Proof of understanding

  • Explain the mechanism from first principles and identify the state it reads or changes.
  • Give one situation where the concept is the right choice and one where it is not.
  • Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
  • Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.

Common mistakes

  • Recomputing the aggregate from scratch instead of updating incrementally
  • Shrinking with an `if` when a `while` is needed to restore the invariant
  • Confusing fixed-size and variable-size window templates

Learn from primary sources

Practice and explain it back

Longest substring without repeats

Find the length of the longest substring with no repeating characters using a variable-size sliding window.

Expected evidence: lengthOfLongestSubstring('abcabcbb') -> 3

Open the interactive drill →

Longest Substring Without Repeating Characters

LeetCode #3 — Longest Substring Without Repeating Characters. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/longest-substring-without-repeating-characters/

Expected evidence: Pass all LeetCode test cases for this problem.

Open the interactive drill →

Review prompts

  • Why must the shrink step be a `while` loop rather than an `if`?

Build evidence

Use a roadmap capstone to turn this concept into working evidence.

Prerequisites

Related concepts

None assigned yet.

Learning paths