Sliding Window Coding Problems: 71 Questions with Solutions

71 sliding window coding problems — 13 easy · 43 medium · 15 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 71
  • By difficulty: 13 easy · 43 medium · 15 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

A window over a contiguous range that grows from the right and shrinks from the left keeps a running answer in linear time: the longest substring without repeats, the smallest subarray reaching a sum, the maximum of every window of size k. The problems here practise the invariant that decides when the window must shrink and the counters that make the check constant time.

How sliding window works, step by step

sp0w1w2k3e4w5leftrightlast seen: p→0 k→3 e→4 w→5longest so far: 3 ("wke")
Longest substring without repeating characters, with a sliding window. Example: s = "pwwkew"
  1. The window is the stretch s[left..right], and it may never hold a character twice. Both edges start at index 0 with nothing inside yet; a map remembers the last index each character was seen at.
  2. right moves to index 0 and reads 'p', which is not in the window, so the window grows to "p" (length 1). That is the longest yet: best = 1.
  3. right moves to index 1 and reads 'w', which is not in the window, so the window grows to "pw" (length 2). That is the longest yet: best = 2.
  4. right reaches 'w' at index 2, but 'w' is already in the window at index 1. Shrinking one step at a time would work, but the map says exactly where the repeat is.
  5. So left jumps straight to index 2, one past the old 'w'. The window "w" is distinct again; its length is 1, and the best is still 2.
  6. right moves to index 3 and reads 'k', which is not in the window, so the window grows to "wk" (length 2). The best stays 2.
  7. right moves to index 4 and reads 'e', which is not in the window, so the window grows to "wke" (length 3). That is the longest yet: best = 3.
  8. right reaches 'w' at index 5, but 'w' is already in the window at index 2. Shrinking one step at a time would work, but the map says exactly where the repeat is.
  9. So left jumps straight to index 3, one past the old 'w'. The window "kew" is distinct again; its length is 3, and the best is still 3.
  10. right has passed the end. Each index entered the window once and left at most once, so the scan is O(n) — the answer is 3, for "wke".

Sliding Window study plan

14 of the 71 Sliding Window problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 57 in the full list below are practice at your own pace. Then move on to Prefix Sum.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Prefix Sum

Sliding Window: the essentials

When to reach for it

A contiguous subarray or substring with a condition on its contents that behaves monotonically: if a window is valid, so is every window inside it (for "longest") or every window around it (for "shortest"). "At most k distinct", "no repeated character", "sum at least the target, all values positive" and "every window of size k" all qualify. With negative numbers a sum condition is not monotone; use a Prefix Sum instead.

The pattern

Advance right one step at a time and add its element to the window's counters. While the window breaks the condition, remove nums[left] and advance left. Once it is valid again, [left, right] is the longest valid window ending at right. In the code below the left edge jumps straight past the earlier copy instead of stepping.

def longest_unique(s):
    last, left, best = {}, 0, 0
    for right, ch in enumerate(s):
        if last.get(ch, -1) >= left:    # a repeat inside the window
            left = last[ch] + 1
        last[ch] = right
        best = max(best, right - left + 1)
    return best

Cost

Each index enters and leaves the window at most once: O(n) time, with O(k) space for counters over k distinct values.

Common mistakes

  • Window length off by one: [left, right] inclusive holds right - left + 1 elements.
  • Recording the answer at the wrong moment: for "longest", after the window is valid again; for "shortest", inside the shrinking loop, while it is still valid.
  • Leaving zero counts in a map and then using the map's size as the number of distinct values.
  • Shrinking a window on sums that can go negative.

Start with

All sliding window problems

Easy (13)

Medium (43)

Hard (15)

Companies that ask sliding window problems

  • Amazon 61 problems on sliding window
  • Google 48 problems on sliding window
  • Microsoft 12 problems on sliding window
  • Meta 7 problems on sliding window
  • Adobe 6 problems on sliding window
  • Flipkart 4 problems on sliding window
  • Zoho 4 problems on sliding window
  • Cognizant 3 problems on sliding window
  • Infosys 3 problems on sliding window
  • TCS 3 problems on sliding window
  • Uber 3 problems on sliding window
  • Accenture 2 problems on sliding window

Next topic: Prefix Sum