DEV Community

Lalit
Lalit

Posted on

Lets master sliding window pattern: an essential DSA technique

Welcome the first post of the our full series of DSA for Frontend Engineer (But I guess it will be useful for all developers). Today, we are diving into simple, yet powerful and very useful technique called Sliding window. This technique is lifesaver for solving problems that involve finding and optimal subarray, substring or subsequence with larger data set.

So, what is Sliding Window pattern?

Imagine you are at a school assembly, and your job is to find the 10 students in a row who have the height total height.

A brute force approach would be pick the first 10 students, measure their total height, then move to the next group of 10(starting from the second student) and measure their total height from scratch, and so on. This would be a lot of repetitive work.

A sliding window approach is much smarter this brute force approach. You would first measure the total height of the first group of 10 students. To find the total height of the next group of 10(students 2 through 11), you don't start over. Instead, you simply subtract the height of the first student (who is now out the group) and add the height of the 11 th student (who is not in the group). You have effectively 'move' or 'slide' your window of 10 students one spot to the right, and you did it with just two quick calculations instead of ten.

This simple optimization is what makes the pattern so powerful. By reusing the information from the previous window, it significantly reduces the no. of operations needed to solve a problem. Often improving the time complexity from an inefficient O(n2) to must faster O(n).

Lets take some example to understand it better

We will try to understand using 5 examples by getting intuition and approach and data structure used.

Problem LeetCode # Technique Used or DS used Key Insight Solution Link
Longest Substring Without Repeating Characters 3 Hash Set + Two Pointers Maintain window of unique characters using set View Solution
Minimum Window Substring 76 Frequency Map + Counter Expand right until valid, then shrink left View Solution
Subarray Product Less Than K 713 Dynamic Window + Running Product Adjust window based on current product View Solution
Sliding Window Maximum 239 Monotonic Deque Maintain decreasing order in deque View Solution
Minimum Size Subarray Sum 209 Dynamic Window + Prefix Sum Expand right, shrink left when sum β‰₯ target View Solution

πŸš€ Problem Catalog

Problem Name LC # Difficulty Key Technique Window Type Time Complexity
Permutation in String 567 Medium Frequency Array Fixed O(n)
Fruit Into Baskets 904 Medium Hash Map Dynamic O(n)
Max Consecutive Ones III 1004 Medium Zero Counter Dynamic O(n)
Sliding Window Maximum 239 Hard Monotonic Deque Fixed O(n)
Minimum Window Substring 76 Hard Frequency Map Dynamic O(n)
Longest Repeating Character Replacement 424 Medium Frequency Count Dynamic O(n)
Subarray Product Less Than K 713 Medium Running Product Dynamic O(n)
Find All Anagrams in a String 438 Medium Frequency Array Fixed O(n)

πŸ”‘ Pattern Cheat Sheet

  1. Fixed Window:

    • Constant size (#239, #567, #438)
    • Template:
     for (let i = 0; i <= nums.length - k; i++) {
         // Process window [i, i+k-1]
     }
    
  2. Dynamic Window:

    • Flexible size (#3, #76, #209)
    • Template:
     while (right < nums.length) {
         // Expand window
         while (invalid) {
             // Shrink window
         }
         // Update answer
     }
    
  3. Hybrid Techniques:

    • Frequency maps (#76, #567)
    • Monotonic queues (#239)
    • Prefix sums (#209)

πŸ“ˆ Recommended Practice Order

  1. Start with #3 β†’ #209 β†’ #567 (basic patterns)
  2. Advance to #904 β†’ #1004 β†’ #424 (intermediate)
  3. Master #239 β†’ #76 β†’ #713 (advanced)

Pro Tip: Focus on identifying the "window validity condition" first in each problem!

Wrapping Up πŸš€

The sliding window technique is one of those must-have tools in every developer's DSA toolkit. As we've seen through these examples:

βœ” Efficiency Boost: Turns O(nΒ²) problems into O(n) magic

βœ” Real-World Relevance: From string manipulation to array analysis

βœ” Pattern Versatility: Adaptable to counting, summing, and max/min problems

Found this helpful?

πŸ‘‰ Star the GitHub repo to bookmark these solutions

πŸ‘‰ Share with your coding buddies who are prepping for interviews

πŸ‘‰ Leave a comment with your favorite sliding window problem!

πŸ€– This article has been assisted by AI | Try using AI to practice problem and ace interviews, not only interviews, to become a better software engineer

Top comments (0)