Skip to main content
Sliding WindowPatterns

How to Master the Sliding Window Pattern

· 6 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

The sliding window technique turns O(n²) brute-force substring problems into clean O(n) solutions. Here is the mental model that makes it click.

The core idea

Most substring and subarray problems ask you to find the longest, shortest, or count of subarrays satisfying some condition. The naive approach is O(n²): for every starting index, try every ending index. The sliding window keeps a "window" of elements and expands or shrinks it based on the constraint — each element enters and exits the window at most once, giving O(n).

Two window types

Fixed-size windows (find max sum of subarray of size k) and variable-size windows (find the longest substring without repeating characters). Fixed windows are simpler: slide one step at a time, adding the new element and subtracting the one that fell off. Variable windows need two pointers — expand the right pointer until the constraint is violated, then shrink the left until it is satisfied again.

The recognition signal

If a problem asks about a contiguous subarray or substring and involves max/min/count with some constraint on the elements inside that range, reach for sliding window first. Common keywords: "longest", "smallest subarray", "at most k distinct", "minimum window containing".

Common mistakes to avoid

The most frequent bug is forgetting to remove the left element's contribution to window state when shrinking. For example, if you track character frequencies, decrement the count for arr[left] before advancing left. A second bug is using a fixed-size loop when the window should be variable — check the problem constraints carefully.

Frequently Asked Questions

What is the sliding window technique in coding interviews?
The sliding window technique maintains a "window" of elements in an array or string and expands or shrinks it based on a constraint. Instead of checking every possible subarray in O(n²), the window lets each element enter and exit at most once, giving O(n) time. It is one of the most common patterns in FAANG coding interviews.
When should I use sliding window vs two pointers?
Use sliding window when you need a contiguous subarray or substring satisfying some constraint (longest, shortest, count). Use two pointers when the array is sorted and you are looking for pairs or triplets. Sliding window is a specialized form of two pointers where both pointers move in the same direction.
What are the most common sliding window LeetCode problems?
The most frequently asked sliding window problems are: Longest Substring Without Repeating Characters (LC 3), Minimum Window Substring (LC 76), Longest Repeating Character Replacement (LC 424), Permutation in String (LC 567), and Sliding Window Maximum (LC 239). Minimum Window Substring is the hardest and most commonly asked at Google and Meta.
How do I know if a problem is a sliding window problem?
Look for these keywords: "contiguous subarray", "substring", "longest/shortest with constraint", "at most k distinct elements", or "minimum window containing". If the problem asks about a range of consecutive elements with some sum or count constraint, sliding window is likely the right approach.
What is the time complexity of the sliding window pattern?
O(n) for both fixed and variable-size windows. Each element enters the window once (right pointer moves right) and exits at most once (left pointer moves right), so the total operations are bounded by 2n, which is O(n). Space complexity is O(k) where k is the size of the window or the number of distinct elements tracked.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee