Roadmap › Stage 2: Arrays & Strings › Pattern 7 of 53
DSA pattern guide

Array — Sliding Window

Contiguous subarray, "longest/shortest/max sum" with a size k or a condition.

Stage 2 · Arrays & Strings8 problems2 Easy · 4 Medium · 2 Hard

Use it when

Contiguous subarray, "longest/shortest/max sum" with a size k or a condition.

Key idea

Grow the right edge, shrink the left edge when the window breaks the rule. Keep a running sum or count instead of recomputing.

Practice problems, in order

  1. Maximum Sum Subarray of Size KEasy
  2. Max Consecutive OnesEasy
  3. Max Consecutive Ones IIIMedium
  4. Subarray Product Less Than KMedium
  5. Fruits Into BasketsMedium
  6. Minimum Size Subarray SumMedium
  7. Sliding Window MaximumHard
  8. Subarray with K Distinct IntegersHard

How to practise this pattern

  1. Read the problem and look for the signal above before writing any code.
  2. Say the brute force out loud, with its time complexity, then ask what work it repeats.
  3. Apply the key idea and write the optimised version yourself.
  4. If you're stuck for more than 10 minutes, take one small hint, not the full solution.
  5. Move to the next problem only when you can explain why this pattern fits.

Stuck on one of these?

The AlgoSolveo extension (free, launching soon) sits beside the LeetCode problem and gives the smallest hint that gets you moving, never the full answer. Solved problems tick off on your roadmap.

Join the extension waitlistTrack it on the roadmap
← Previous patternArray — Two PointersNext pattern →Array — Prefix Sum