Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Introduction to Sliding Window Pattern

Core idea

How it works

Recognize it when

The template

Variants in this chapter

Watch out for

How this compares with nearby patterns

Key takeaways

You are given an array and a number K. Find the average of every group of K neighbouring values.

[1, 3, 2, 6, -1, 4, 1, 8, 2]     K = 5

The word neighbouring is important. The values in each group must be next to one another. Such a group is called a contiguous subarray.

A simple solution starts at every index. It adds the next K values and divides the sum by K.

This repeats a large amount of work. For every starting position, it performs K additions. The time complexity is O(N * K).

Now compare the first two groups:

[1, 3, 2, 6, -1]
   [3, 2, 6, -1, 4]

Four values appear in both groups. We should not add those four values again.

Instead, keep the sum of the current group. When the group moves one step right:

  1. Subtract the value that leaves from the left.
  2. Add the new value that enters from the right.

This moving group is called a window. Updating its two edges changes O(N * K) work into O(N) work.

Core idea

The Sliding Window pattern keeps a contiguous range over an array or string.

The window has a left boundary and a right boundary. It also keeps a small summary of the values inside it.

The summary may be a sum, a count, a product, or a frequency map. We update the summary when values enter or leave the window.

How it works

For a fixed window of size K, follow these steps:

  1. Set the running sum to zero.
  2. Move the right boundary through the array.
  3. Add the new value to the running sum.
  4. When the window contains K values, record the average.
  5. Subtract the value at the left boundary.
  6. Move the left boundary one step right.
  7. Continue until the right boundary reaches the end.
One running sum over a window of five, updated only at the two edges as it moves.
One running sum over a window of five, updated only at the two edges as it moves.

Some problems do not give a fixed size. The window grows while it is valid. When it becomes invalid, move the left boundary until it is valid again.

Each value enters the window at most once. Each value also leaves at most once. Both boundaries move only from left to right.

Therefore, the total time complexity is usually O(N).

A variable-size solution may contain a loop inside another loop. It is still O(N) because the left boundary never returns to an earlier index.

Recognize it when

Sliding Window may be useful when:

  • The problem asks about a contiguous subarray or contiguous substring.
  • The question asks for the longest or shortest valid range.
  • The question asks for the maximum or minimum value across ranges.
  • The problem gives a fixed size such as K.
  • Your first solution starts at each index and scans forward again.
  • A small summary can describe the current range.

Words such as "consecutive," "neighbouring," and "substring" are strong signals. They tell you that values cannot be skipped.

Do not use this pattern when:

  • Values may be skipped. That creates a subsequence and often needs Dynamic Programming.
  • You need a pair or triplet in a sorted array. Use Two Pointers.
  • You need answers for many independent range-sum queries. Use Prefix Sum.
  • You need the maximum or minimum value inside every window. A Monotonic Queue may be better.

The template

This template handles a window whose size changes:

function slidingWindow(input): windowStart = 0 summary = empty best = starting value for windowEnd from 0 to length(input) - 1: add input[windowEnd] to summary while window is not valid: remove input[windowStart] from summary windowStart = windowStart + 1 best = update answer using current window return best

Three parts change from problem to problem:

  1. The summary that describes the window.
  2. The condition that says whether the window is valid.
  3. The answer that you record.

Use while when shrinking a variable-size window. One removal may not make the window valid again.

For a fixed-size window, shrink when its length becomes greater than K.

The current window length is:

windowEnd - windowStart + 1

Variants in this chapter

VariantHow the window behavesProblems in this chapter
Fixed sizethe window always contains K valuesMaximum Sum Subarray of Size K
Smallest valid windowgrow until valid, then shrink while it remains validSmallest Subarray With a Greater Sum
Largest valid windowgrow normally and shrink when the window becomes invalidLongest Substring with K Distinct Characters, Fruits into Baskets, Longest Substring with Same Letters after Replacement, Longest Subarray with Ones after Replacement
Frequency matchinga map tracks how many times each character appearsPermutation in a String, String Anagrams, Smallest Window containing Substring, Words Concatenation
Count valid windowsevery valid window adds to a totalCounting Subarrays with Product Less than a Target, Subarrays with Product Less than a Target

Start with the fixed-size version. Then learn how to grow and shrink a variable-size window.

Watch out for

  • Using if instead of while. A variable-size window may need several removals.
  • Forgetting the outgoing value. Remove it from the sum, count, product, or frequency map.
  • Using the wrong window length. Include + 1 because both boundary positions belong to the window.
  • Deleting a frequency too early. Reduce its count first, and delete the key only when the count becomes zero.
  • Scanning the full frequency map on every step. Keep a separate count of satisfied requirements when possible.
  • Calling the algorithm O(N²) because it has two loops. Each boundary moves at most N times.

How this compares with nearby patterns

If the problem needs...Use...
a contiguous range that grows or shrinksSliding Window
a pair or triplet in a sorted arrayTwo Pointers
sums for many separate fixed rangesPrefix Sum
a maximum or minimum inside every windowMonotonic Queue

Sliding Window and Two Pointers both use two indices. Their indices represent different things.

In Sliding Window, the indices are the boundaries of one contiguous range. In Two Pointers, the indices usually identify two values being compared.

Key takeaways

  • A sliding window always represents a contiguous range.
  • Update a small summary when values enter or leave the window.
  • Each boundary moves only forward, so the total work is usually O(N).
  • Use while when one removal may not make a variable-size window valid.
  • The summary, validity condition, and recorded answer change between problems.
  • If values may be skipped, Sliding Window is usually not the correct pattern.

Now apply this pattern to Maximum Sum Subarray of Size K.

Priyanka Bhosale

Priyanka Bhosale

· 2 years ago

Is this course still available for me to access after my monthly subscription expires?

Show 1 reply
A

aj

· 4 years ago

How is the complexity of brute force O(N*K)? N is the number of elements in input array - which in the example is 9. But the outer loop doesn't run 9 times does it? It will only run from 0 to arr.length - K i.e 4 times.

Show 2 replies
A

Adam Woolhether

· 4 years ago

I'll be writing my solutions in Go, anyone interested in reviewing with me?

S

srilu

· 4 years ago

In the Brute Force Python Solution why is "for i in range(len(arr)-K+1) " used instead of just " for i in range(K)" ? Aren't these two conditions the same thing?

Show 3 replies
D

Daniel Lim

· 4 years ago

What is the difference between these two ranges of for loops:

for i in range(len(arr)-K+1)

and

for i in range(K)

Show 1 reply
J

joe

· 5 years ago

sum in the O(n * k) element JS solution wasn't initialized properly as 'let sum'

O

ornella

· 5 years ago

WindowStart is not in the for statement, so it shouldn't iterate, so how it is moving across the array?

Show 1 reply
O

ornella

· 5 years ago

Why windowSum is a float?

Show 1 reply

Reading Progress

0%


Vote for new content

On This Page

Core idea

How it works

Recognize it when

The template

Variants in this chapter

Watch out for

How this compares with nearby patterns

Key takeaways