Skip to main content
Interview

The sliding window technique - when to use it and common patterns

Kavya MalhotraKavya Malhotra
7/23/2025
193

I struggled with sliding window for the longest time until I realized there are basically 3 patterns:

Fixed size window: Easy. Just maintain window of size k.

  • Max sum subarray of size k
  • First negative in every window

Variable size (find maximum):

  • Longest substring without repeating characters
  • Longest substring with at most k distinct characters
  • Max consecutive ones with at most k flips

Variable size (find minimum):

  • Minimum window substring
  • Smallest subarray with sum >= target

Once you categorize, the approach becomes mechanical. Anyone have additions to this?


sliding-windowpatternsdsatutorial

Comments (1)

Sign in to join the discussion.
Lakshay Desai
Lakshay Desai7/25/2025

Cool breakdown Kavya! But what about the cases where you need to find the exact subarray instead of just the length? Like in the variable size patterns.