Level Up Your Coding Skills & Crack Interviews — Save up to 50% or more on Educative.io Today! Claim Discount

Arrow
Table of contents

Sliding Window

The sliding window technique is an elegant algorithmic approach that transforms certain problems from brute-force solutions with quadratic complexity or worse into efficient linear-time solutions.

You’ll see a sliding window pop up in classic problems like finding the maximum sum of a subarray or the longest substring without repeating characters, and even in more advanced scenarios like streaming data analysis. If you’ve ever needed to process “chunks” of data that overlap, you’ve probably brushed up against this technique.

Note: To get the most out of sliding window problems, you’ll want a solid grasp of basic programming, especially working with arrays and pointers. If you’re comfortable with for-loops and indexing, you’re in good shape.

What is the Sliding Window technique?

At its core, the sliding window technique involves maintaining a “window”, a subset of elements from a larger dataset, that slides across the data structure from left to right. Rather than recalculating everything for each possible subset, the window efficiently updates by adding new elements on one end and removing old elements from the other.

1 / 4

Understanding Sliding Window with a real-world example

To better understand a sliding window, imagine looking through a physical window at a scenic landscape. As you walk alongside a wall, your view changes: new scenery appears on one side while the view on the other side disappears. The sliding window algorithm works similarly, maintaining a dynamic view of a portion of your data.

It uses two pointers to mark the window’s boundaries. As these pointers traverse an array or string, they define which portion of the data is currently being examined.

The window updates dynamically, new elements enter as the pointers advance, while old elements exit, maintaining an efficient view of the current subset.

Types of sliding window

The sliding window technique is built on two fundamental concepts: the window’s size and how its boundaries move. These concepts give rise to two primary approaches:

Fixed-size windows

Fixed-size windows maintain a constant width as they traverse the data, with both pointers moving together to keep the window size unchanged. These are useful when you need to examine all subarrays or substrings of a specific length.

For example, if you need to find the largest sum of three consecutive numbers, the window size stays at three while sliding across the array.

1 / 5

Variable-size windows

Variable-size windows allow independent pointer movement, with one pointer (usually the right) expanding the window while the other (left) contracts it based on certain conditions. These are powerful for problems where you need to find the smallest or largest subarray that satisfies specific criteria. A classic example that uses variable-sized windows is finding the smallest subarray with a sum greater than a target value.

Tip: The sliding window technique is especially handy when you need to maintain a running property (like a sum, count, or set of unique elements) as you move through the data.

Python implementation

Here’s a Python implementation that finds the maximum sum of any subarray using a fixed sliding window:

When to use a sliding window?

You should strongly consider a sliding window when a coding problem has any of the following signals:

  • The problem describes contiguous sequences:
    • The prompt mentions “consecutive elements,” “substring,” or “subarray.”
    • You need to find a subarray with a specific sum, product, or other aggregate property.
    • The task resembles finding an optimal window, such as smallest subarray with sum ≥ target.
  • The task involves optimization with conditions:
    • It asks for maximum, minimum, longest, or shortest among all windows of a certain size or condition.
    • The problem involves “at most k” or “at least k” of something within a contiguous range.
    • The input includes phrases like “all anagrams,” “permutation in string,” or “repeating characters.”
  • Efficiency is a concern:
    • You need to track frequencies or counts of elements within a range that shifts.
    • The constraints suggest that checking every possible subarray with nested loops would be too slow.

Common pitfalls and edge cases

These mistakes frequently cause incorrect answers or timeouts.

  1. You should not move the left pointer backward in variable-size windows, because this violates the linear time guarantee and can cause infinite loops.
  2. You should not forget to update your result before or after adjusting the window, because the optimal answer might be found mid-slide.
  3. You should handle edge cases like empty arrays, single-element arrays, or windows larger than the array itself.
  4. You should be careful with window initialization, ensuring you process the first window correctly before sliding.
  5. You should choose the right condition for expanding versus contracting the window in variable-size problems, because reversing these leads to wrong answers.
  6. You should not confuse “at most k” with “exactly k” conditions, because they require different window adjustment logic.

Frequently Asked Questions

When should I use a fixed window versus a variable window?

Use a fixed window when the problem specifies an exact size, such as “subarray of length k.” Use a variable window when the problem asks for the optimal size meeting certain conditions, such as “smallest subarray with sum ≥ target.”

How do I know when to expand or contract a variable window?

Expand the window (move the right pointer) when the current window doesn’t satisfy the condition. Contract the window (move the left pointer) when it satisfies the condition, and you want to find a smaller valid window, or when it violates a constraint like “at most k distinct characters.”

Can a sliding window be applied to linked lists?

Yes, but it’s less common because you cannot easily access arbitrary positions. You typically use two pointers moving at different speeds instead of maintaining explicit window boundaries.

What data structures pair well with a sliding window?

Hash maps are excellent for tracking character or element frequencies within the window. Deques work well when you need to efficiently maintain minimum or maximum values. Arrays can serve as frequency counters when the element range is small.

Share with others:

Unlock up to 68% off lifetime access to Coding Interview prep with Educative

Getting ready for coding interviews or sharpening your problem-solving skills? Unlock a lifetime discount with comprehensive resources designed to help you master technical interviews.

Data structures and algorithms

Pattern-based problem solving

Mock interview practice

Real-world coding challenges

Coding Interview Logo