DEV Community

Preethii V
Preethii V

Posted on

🏏 The 18-Day DSA Cover Drive | Day 3 — Sliding Window: Stop Recalculating, Start Sliding

🏏 The 18-Day DSA Cover Drive | Day 3 — Sliding Window: Stop Recalculating, Start Sliding

Learn DSA one pattern at a time.

What's up, DEV Community! 👋

Welcome back to Day 18 of The 18-Day DSA Cover Drive.


🏆 Kohli Corner

Legacy isn't built in one innings.

Virat Kohli's cover drive wasn't perfected overnight.

Thousands of practice balls.

Hundreds of innings.

One unforgettable shot.

The same goes for DSA. Great problem-solving isn't about memorizing solutions. It's about training your thinking until patterns become instinct.


📊 Day 3 — Sliding Window

🎯 Today's DSA Power-Up

Imagine you're solving this problem:

Find the maximum sum of any subarray of size k.

Your first instinct?

Probably nested loops.

Mine too. 😅


❌ The Brute Force Mindset

For every starting position...

Calculate the sum again.

And again.

And again.

max_sum = 0

for i in range(len(arr) - k + 1):
    current_sum = 0

    for j in range(i, i + k):
        current_sum += arr[j]

    max_sum = max(max_sum, current_sum)
Enter fullscreen mode Exit fullscreen mode

Works?

✅ Yes.

Efficient?

❌ Not really.

Every time the window moves...

We're recalculating values we already know.

Time Complexity:

O(n²)
Enter fullscreen mode Exit fullscreen mode

🤔 Imagine This...

Imagine you're sitting beside a train window.

As the train moves...

Do you look at the entire city again?

No.

You only notice:

🚆 What's leaving your view.

🚆 What's entering your view.

That's exactly how the Sliding Window pattern works.


🚀 The Better Approach

Instead of calculating everything again...

Keep the current sum.

When the window moves:

➖ Remove the left element.

➕ Add the new right element.

That's it.

window_sum = 0
max_sum = 0
left = 0

for right in range(len(arr)):

    window_sum += arr[right]

    if right >= k - 1:

        max_sum = max(max_sum, window_sum)

        window_sum -= arr[left]

        left += 1
Enter fullscreen mode Exit fullscreen mode

Look closely.

No nested loops.

Just two pointers working together.


🎯 What's Really Happening?

Every iteration does only two things:

✅ Add one element.

✅ Remove one element.

The window keeps sliding.

No repeated work.


⚡ Why is it Faster?

Brute Force

For every index
      ↓
Calculate the whole window again
Enter fullscreen mode Exit fullscreen mode

Time Complexity

O(n²)
Enter fullscreen mode Exit fullscreen mode

Sliding Window

Move Right →
Remove Left ←
Enter fullscreen mode Exit fullscreen mode

Every element is visited only once.

Time Complexity

O(n)
Enter fullscreen mode Exit fullscreen mode

That's a massive improvement.


💼 Interview Insight

Whenever you see words like:

  • Subarray
  • Substring
  • Contiguous
  • Window size k
  • Longest
  • Shortest
  • Maximum
  • Minimum

🚨 Pause.

Before writing nested loops...

Ask yourself:

"Can I slide a window instead?"

Many interview questions become much simpler with this mindset.


🧠 Quick Challenge

Can you solve this using Sliding Window?

Find the maximum sum of a subarray of size 3.

arr = [2, 1, 5, 1, 3, 2]
k = 3
Enter fullscreen mode Exit fullscreen mode

👇 Drop your answer in the comments before running the code.

Bonus Question:

Can you think of another problem where Sliding Window can replace nested loops?


🏏 Cover Drive of the Day

🏏 Pattern: Sliding Window

Mindset: Don't recalculate. Slide.

🧠 Memory Trick: Train Window / Cricket Camera

💼 Interview Insight: Whenever you see contiguous arrays or substrings, think Sliding Window before nested loops.

⏭️ Next Innings: Two Pointers — Two indices. One elegant solution.


💬 Let's Discuss

When did the Sliding Window pattern finally click for you?

Or...

Are you still getting confused between Sliding Window and Two Pointers?

Let's discuss in the comments! 👇


If you enjoyed this post and want to follow my 18-Day DSA Cover Drive journey, let's connect!

💼 LinkedIn: https://www.linkedin.com/in/PreethiiV

Happy Coding! 🚀

18DayDSACoverDrive | Python | DSA | Algorithms | Beginners

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.