Identifying the fixed sized sliding window pattern


The fixed-size sliding window technique can only be applied to some specific problems. These are generally easy or medium problems where we must remember some or all occurrences of data items in all windows of a fixed size (a contiguous subarray of length k). Once we map all data items in a window to some values (for example, their frequency in the window), we use the hash map to solve the problem partially or completely.

If the problem statement or its solution follows the generic template below, it can be solved by applying the fixed-sized sliding window technique.

Template:
Given an iterable sequence of data, for all windows of size k, map all the data items in the window to some values and use them to solve the problem.

Example

Let's consider the following problem as an example to better understand how to identify and solve a problem using the fixed-sized sliding window technique.

Problem statement: Given an array of integers `arr` and an integer `k`, return `true` if there are any duplicates in any subarray of size `k`

Liking the course? Check our discounted plans to continue learning.