Sliding Window Cheat Sheet

You grow a window from one end of a list or string and shrink it from the other. You keep a running total so you never add up the same numbers twice.

Sliding Window cheat sheet preview

Unlock the full sheet below.

When to use it

  • You need the longest or shortest stretch of items in a row.
  • You can keep a running total as the window moves.
  • The window either has a fixed size or grows and shrinks to fit a condition.
  • The problem says something like 'at most k' about a stretch of items.

What's on the sheet

The sheet has a code template you can reuse for every Sliding Window problem. It's split into four steps so you can see which part changes from problem to problem. It also shows how to spot the pattern, how fast it runs, the mistakes people often make and the variations you'll see in interviews.

How to get it

You can unlock it with a free account by solving 2 of this pattern's problems.

Preview the Two Pointers sample to see a finished sheet first.