Sliding Window Basics

Sliding Window is one of the most important algorithmic techniques for solving problems involving continuous subarrays or substrings. It appears extremely frequently in coding interviews, especially for roles that emphasize problem-solving and data structures.

If a problem has the following two characteristics, Sliding Window should be your first instinct:

  • The input is linear (array or string)
  • The problem focuses on contiguous ranges

What Is a Sliding Window?

A sliding window maintains a dynamic interval [left, right) over an array or string.

  • right expands the window to include new elements
  • left shrinks the window when constraints are violated
  • The window slides forward while maintaining certain conditions

The key advantage is performance:

Sliding Window often reduces a brute-force O(n²) solution into an efficient O(n) algorithm.

The Core Sliding Window Template

Nearly all sliding window problems can be solved by adapting the following template:

Java
int left = 0, right = 0;

while (right < n) {
    // 1. Expand the window
    add(nums[right]);
    right++;

    // 2. Shrink the window if needed
    while (window needs shrink) {
        remove(nums[left]);
        left++;
    }

    // 3. Update the answer
    updateAnswer();
}

Important Order (Very Common Interview Bug)

Always follow this sequence:

  1. Expand with right
  2. Shrink with left only if needed
  3. Update the result

Reordering these steps is one of the most common causes of incorrect solutions.

Types of Sliding Window Problems

1. Fixed-Size Window

The window size is constant.

Typical examples:

  • Maximum sum of a subarray of size k
  • Average of k consecutive elements

Key characteristics:

  • Window size = right - left == k
  • Move both pointers together

Example logic:

Java
if (right - left == k) {
    updateAnswer();
    remove(nums[left]);
    left++;
}

2. Variable-Size Window (Most Important)

This is the most common interview scenario.

Look for keywords like:

  • at most, at least
  • longest, shortest
  • contains, covers, satisfies

Examples:

  • Longest substring without repeating characters
  • Minimum window substring
  • Shortest subarray with sum ≥ target

The core idea:

  • Expand the window with right
  • Once the condition is satisfied, shrink with left as much as possible

Example: Longest Substring Without Repeating Characters

Problem summary: Given a string s, find the length of the longest substring with no repeated characters.

Thought process:

  • Use right to add characters
  • Maintain window state using a Set or Map
  • If a duplicate appears, move left until the window becomes valid again

Pseudocode structure:

Java
while (right < s.length()) {
    char c = s.charAt(right);
    right++;

    add c to window;

    while (window is invalid) {
        remove s.charAt(left);
        left++;
    }

    update max length;
}

Notice how the structure matches the template exactly.

The Real Difficulty of Sliding Window

The challenge is not writing the code.

The real difficulty lies in defining:

  1. What state does the window maintain?
    (count, sum, map, set, frequency array, etc.)

  2. When is the window invalid?
    (greater than, equal to, less than some constraint)

Once these are clear, the implementation becomes mechanical.

Sliding Window vs Two Pointers

Many candidates confuse these two concepts.

The relationship is simple: Sliding Window is a specialized form of Two Pointers.

TechniqueHas window semanticsMaintains state
Two PointersNot alwaysNot always
Sliding WindowAlwaysAlways

If you are maintaining a range with constraints, it is a sliding window.

Interview Recognition Checklist

When reading a problem, quickly ask yourself:

  1. Is the input continuous?
  2. Is it a subarray or substring?
  3. Does the problem ask for min / max / count?

If the answer is yes to all three → Sliding Window.

Recommended Follow-Up Topics for This Section

After Sliding Window Basics, this topic naturally expands into a full series:

  • Fixed-Size Sliding Window
  • Variable Window – Longest Problems
  • Variable Window – Shortest Problems
  • Sliding Window + HashMap Patterns
  • Common Sliding Window Pitfalls and Debugging Tips

This structure works extremely well for interview prep and SEO-driven technical content.