Sign inStart free →

Sliding window technique: concepts, examples, and practice

29 November 2026 · giveinterview team

Why interviewers ask about it

The sliding window technique is a popular algorithmic approach used to solve problems involving arrays or lists, particularly when dealing with contiguous subarrays. Interviewers often ask about it because it demonstrates a candidate's ability to optimize solutions for time and space complexity. By using this technique, candidates can show their understanding of efficient data processing and their ability to think critically about problem-solving strategies.

The core ideas

The sliding window technique involves maintaining a subset of elements within a larger dataset, adjusting the size of that subset dynamically as you traverse the dataset. The core ideas include:

A worked example

Let's consider a common problem: finding the longest substring without repeating characters. Given a string, you want to find the length of the longest substring that contains all unique characters. Here’s how you can solve it using the sliding window technique:

def length_of_longest_substring(s: str) -> int:
    char_set = set()
    left = 0
    max_length = 0

    for right in range(len(s)):
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        char_set.add(s[right])
        max_length = max(max_length, right - left + 1)

    return max_length

Explanation:

  1. We use a set to track characters in the current window.
  2. The right pointer expands the window by adding new characters.
  3. If a character is already in the set, we shrink the window from the left until all characters are unique.
  4. We continuously update the maximum length found.

5 questions you will be asked

  1. What is the sliding window technique?
  1. How do you decide when to expand or contract the window?
  1. Can you give an example of a problem where sliding window is not applicable?
  1. What are the space complexity implications of using sliding window?
  1. What are some variations of the sliding window technique?

Common mistakes

  1. Not resetting the left pointer: Failing to correctly move the left pointer when duplicates are found can lead to incorrect results.
  2. Ignoring edge cases: Overlooking special cases, such as empty strings or strings with all unique characters, can cause errors.
  3. Mismanaging window size: Not properly calculating the size of the window can lead to incorrect maximum lengths or sums.
  4. Overcomplicating the solution: Attempting to use nested loops instead of maintaining a simple sliding window can increase time complexity unnecessarily.
  5. Not using suitable data structures: Using inefficient data structures for tracking elements can slow down the algorithm.
  6. Failing to clarify problem constraints: Not fully understanding the problem requirements can lead to incorrect assumptions about how to adjust the window.

A 3-day practice plan

Day 1: Basic Problems

Day 2: Intermediate Problems

Day 3: Advanced Problems and Review

New tech roles today

Practise it out loud

Knowing the answer is not the same as explaining it under follow-up questions. Try a free mock interview on giveinterview.com.

Practise it out loud

Reading helps. Saying it to an interviewer who asks follow-ups helps more.