<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rishi Shah</title>
    <description>The latest articles on DEV Community by Rishi Shah (@rishi_shah_291cf6c8c5f4d7).</description>
    <link>https://dev.to/rishi_shah_291cf6c8c5f4d7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3721983%2F9a8c7dce-41ca-4c53-924e-6de1e6064cea.jpg</url>
      <title>DEV Community: Rishi Shah</title>
      <link>https://dev.to/rishi_shah_291cf6c8c5f4d7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rishi_shah_291cf6c8c5f4d7"/>
    <language>en</language>
    <item>
      <title>Mastering the Sliding Window Technique in Python</title>
      <dc:creator>Rishi Shah</dc:creator>
      <pubDate>Wed, 21 Jan 2026 13:30:39 +0000</pubDate>
      <link>https://dev.to/rishi_shah_291cf6c8c5f4d7/mastering-the-sliding-window-technique-in-python-39g2</link>
      <guid>https://dev.to/rishi_shah_291cf6c8c5f4d7/mastering-the-sliding-window-technique-in-python-39g2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stop using nested loops for subarray problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most common patterns in coding interviews is the "Subarray" problem. If you see a question asking for the "maximum sum of a subarray of size K," your instinct might be to use nested loops.&lt;/p&gt;

&lt;p&gt;However, that approach is often too slow (O(N²)) and will cause a "Time Limit Exceeded" error on large test cases. Today, I'll explain the Sliding Window technique, which optimizes this to linear time (O(N)).&lt;/p&gt;

&lt;p&gt;The Problem&lt;br&gt;
Given an array of integers, find the maximum sum of a subarray of size 'k'.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
The Naive Approach (Don't do this)
The intuitive way is to calculate the sum for every possible subarray.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def max_sum_naive(arr, k):
    max_sum = 0
    # Loop through the array
    for i in range(len(arr) - k + 1):
        current_sum = 0
        # Re-calculate sum for every window
        for j in range(i, i + k):
            current_sum += arr[j]
        max_sum = max(max_sum, current_sum)
    return max_sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Why this fails: If k is large, we are re-adding the same numbers over and over again. We are doing redundant work.&lt;/p&gt;

&lt;p&gt;2.&lt;br&gt;
The Sliding Window Approach&lt;br&gt;
Imagine a window frame of size k sitting on the array. To move the window one step to the right, we don't need to recalculate everything.&lt;br&gt;
We simply:&lt;br&gt;
Subtract the element that is leaving the window (the one on the left).&lt;br&gt;
Add the element that is entering the window (the one on the right).&lt;/p&gt;

&lt;p&gt;This converts the logic from a nested loop to a single pass.&lt;/p&gt;

&lt;p&gt;3.&lt;br&gt;
The Optimized Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def max_sum_sliding_window(arr, k):
    # Edge case: if array is smaller than window size
    if len(arr) &amp;lt; k:
        return -1

    # Calculate sum of the very first window
    window_sum = sum(arr[:k])
    max_sum = window_sum

    # Slide the window across the rest of the array
    for i in range(len(arr) - k):
        # Subtract the previous element, add the next element
        window_sum = window_sum - arr[i] + arr[i + k]
        max_sum = max(max_sum, window_sum)

    return max_sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.&lt;/p&gt;

&lt;p&gt;Complexity Analysis&lt;br&gt;
Naive Approach: O(N x K). If K is close to N, this becomes O(N²).&lt;br&gt;
Sliding Window: O(N). We traverse the array exactly once.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
The Sliding Window technique is a fundamental pattern for arrays and strings. By avoiding redundant calculations, we reduced the complexity significantly. In an interview, this optimization is usually the difference between passing and failing.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>coding</category>
    </item>
    <item>
      <title>Mastering the Sliding Window Technique in Python</title>
      <dc:creator>Rishi Shah</dc:creator>
      <pubDate>Tue, 20 Jan 2026 15:01:03 +0000</pubDate>
      <link>https://dev.to/rishi_shah_291cf6c8c5f4d7/mastering-the-sliding-window-technique-in-python-38na</link>
      <guid>https://dev.to/rishi_shah_291cf6c8c5f4d7/mastering-the-sliding-window-technique-in-python-38na</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stop using nested loops for subarray problems.&lt;/strong&gt;&lt;br&gt;
One of the most common patterns in coding interviews is the "Subarray" problem. If you see a question asking for the "maximum sum of a subarray of size K," your instinct might be to use nested loops.&lt;br&gt;
However, that approach is often too slow (O(N²)) and will cause a "Time Limit Exceeded" error on large test cases. Today, I'll explain the Sliding Window technique, which optimizes this to linear time (O(N)).&lt;br&gt;
The Problem&lt;br&gt;
Given an array of integers, find the maximum sum of a subarray of size 'k'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The Naive Approach (Don't do this)&lt;/strong&gt;&lt;br&gt;
The intuitive way is to calculate the sum for every possible subarray.&lt;br&gt;
Python&lt;br&gt;
&lt;code&gt;def max_sum_naive(arr, k):&lt;br&gt;
    max_sum = 0&lt;br&gt;
    # Loop through the array&lt;br&gt;
    for i in range(len(arr) - k + 1):&lt;br&gt;
        current_sum = 0&lt;br&gt;
        # Re-calculate sum for every window&lt;br&gt;
        for j in range(i, i + k):&lt;br&gt;
            current_sum += arr[j]&lt;br&gt;
        max_sum = max(max_sum, current_sum)&lt;br&gt;
    return max_sum&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why this fails: If k is large, we are re-adding the same numbers over and over again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Sliding Window Approach&lt;/strong&gt;&lt;br&gt;
Imagine a window frame of size k sitting on the array. To move the window one step to the right, we don't need to recalculate everything. We simply:&lt;br&gt;
Subtract the element that is leaving the window (the one on the left).&lt;br&gt;
Add the element that is entering the window (the one on the right).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. The Optimized Code&lt;/strong&gt;&lt;br&gt;
Python&lt;/p&gt;

&lt;p&gt;`def max_sum_sliding_window(arr, k):&lt;br&gt;
    # Edge case&lt;br&gt;
    if len(arr) &amp;lt; k:&lt;br&gt;
        return -1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Calculate sum of the very first window
window_sum = sum(arr[:k])
max_sum = window_sum

# Slide the window across the rest of the array
for i in range(len(arr) - k):
    # Subtract the previous element, add the next element
    window_sum = window_sum - arr[i] + arr[i + k]
    max_sum = max(max_sum, window_sum)

return max_sum`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Complexity Analysis
Naive Approach: O(N x K). If K is close to N, this becomes O(N²).
Sliding Window: O(N). We traverse the array exactly once.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Conclusion&lt;br&gt;
The Sliding Window technique is a fundamental pattern for arrays and strings. By avoiding redundant calculations, we reduced the complexity significantly. In an interview, this optimization is the difference between passing and failing.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
