<aside>
This section is part of the 5-hour LeetCode DSA Foundations course by Mukul Raina
Section 7: Sliding Window
When a problem asks you to find the longest, shortest, or optimal contiguous subarray or substring that satisfies some condition, and the brute force involves checking every possible subarray, sliding window is almost always the O(n) path to the answer. It is one of the highest-frequency interview patterns across Google, Meta, Amazon, and Microsoft.
The idea: instead of recomputing the answer from scratch for every possible subarray, maintain a “window” over the data and update it incrementally. Expanding the window adds one element. Shrinking the window removes one element. Each update costs O(1), and the window slides across the entire input in O(n) total.
What We’ll Cover
Where This Section Connects
| Section | Connection |
|---|---|
| Arrays (Section 3) | Subarray patterns introduced prefix sums and Kadane’s. Sliding window is the third core subarray technique. |
| Strings (Section 4) | “Substring” in a string problem IS “subarray” on the character array. Same templates apply directly. |
| Hash Maps (Section 5) | Sliding window frequently uses HashMap or HashSet to track window contents. |
| Two Pointers (Section 6) | Sliding window is a specialized form of same-direction two pointers, where left and right define a window with tracked state. |
If you understood the two-pointer idea from Section 6, and the frequency tracking from Section 5, sliding window is where they combine into one of the most powerful interview techniques.
<aside>
Mukul Raina | youtube.com/@mukulraina | linkedin.com/in/mukulraina
</aside>
Why Brute Force Is Wasteful