All puzzles › Sliding Window

Sliding Window Practice Problems

13 puzzles (2 easy, 9 medium, 2 hard), 2 free, in Python, JavaScript and C++.

A sliding window is a stretch of an array or string that you grow on the right and shrink on the left, keeping a running summary of what is inside. Because each element enters once and leaves once, a question that looks like it needs every possible subarray is answered in one pass.

How to spot a sliding window problem

The words to listen for are contiguous, subarray, substring, and longest or shortest. If the problem asks for the best run of neighboring elements that satisfies some limit, and adding an element can only make the window more full while removing one makes it less, it is a window.

New to it? Read the lesson first: Sliding Window.

The puzzles, easiest first

  1. Find the Stutter (Easy): A stutter is a run of slow frames, and players feel it even when the average frame rate looks fine.
  2. KV Cache Window (Easy): Given a stream of keys and a max length, return what's left in the cache, oldest first.
  3. Cut Where the Data Says (Medium, free): A backup tool splits files into chunks and stores each distinct chunk once.
  4. Longest Unique Substring (Medium, free): Given a string, return the length of the longest stretch with no repeated characters.
  5. 1D Convolution (Medium): Given a signal and a smaller kernel, slide the kernel along the signal and return the weighted sum at each spot where it fully fits, rounded to 3 decimals.
  6. How Many Got Through (Medium): Given sorted request times, a window length, and a limit, return how many requests are allowed.
  7. Keep the Combo Alive (Medium): A fighting game rewards a combo: hits landed close together.
  8. Max Pooling (1D) (Medium): Given a list of numbers and a window size, split the list into chunks of that size, with no overlap, and return the biggest value in each chunk.
  9. Mean Pooling (1D) (Medium): Given a list of numbers and a window size, split the list into chunks of that size, with no overlap, and return the average of each chunk, rounded to 3 decimals.
  10. Overlapping RAG Chunks (Medium): Given a list of tokens, a chunk size, and an overlap, split the tokens into chunks that share overlap tokens with the chunk before.
  11. Room for It (Medium): Given a memory bitmap where 0 is a free page and 1 is used, and a number k, return the starting index of the first run of k free pages in a row, or -1 if there isn't one.
  12. Moving Average (Hard): Given readings and a window size k, return the average of every run of k readings in a row, rounded down.
  13. Sliding Window Maximum (Hard): Given an array and a window size k, return the biggest number in every run of k numbers in a row.

Practice another pattern

Or browse by subject

Browse all 676 puzzles