A Bird’s Eye View into Sliding Windows
Objective: In this lesson, we'll cover this concept, and focus on these outcomes:
- You'll learn what the sliding windows algorithm pattern is.
- We'll show you how and when to use sliding windows in programming interviews.
- We'll walk you through some examples.
A sliding window is a sublist or subarray that runs over an underlying data structure
. The data structure is typically iterable and ordered, such as an array
or a string
.
At a high level, you can think of it as a subset of the two pointers
method. It normally encompasses searching for a longest, shortest or optimal sequence that satisfies a given condition.
Most sliding windows problems can be solved in O(N)
time and O(1)
space complexity.
Say that we have an array that looks like this:
1const arr = ['a', 'l', 'g', 'o', 'd', 'a', 'i', 'l', 'y'];