Sliding Window Interview Questions
February 26, 2026 • By Surya Singh • DSA • Sliding Window • Arrays • Strings • Interview
Sliding window pattern interview questions and solutions for arrays and strings.
Key Takeaways
- 1Sliding window optimizes substring/subarray problems from O(n²) to O(n) by avoiding redundant work.
- 2Fixed-size window: maintain a window of length k; variable-size: expand/shrink based on a condition.
- 3Use two pointers (left, right) or a deque for efficient min/max in the window.
- 4Classic problems: max sum subarray of size k, longest substring with k distinct chars, min window substring.
The questions below are commonly asked in technical interviews. Each answer is written to help you understand the concept clearly and explain it confidently. Focus on understanding the "why" behind each answer—that is what interviewers care about.
In this guide
- 1. What is the sliding window pattern and when should I use it?
- 2. How do I find the maximum sum of any subarray of size k?
- 3. What is the variable-size sliding window and when do I shrin…
- 4. How do I solve "longest substring with at most k distinct ch…
- 5. When should I use a deque instead of two pointers for slidin…
- Related Interview Guides
Interview Questions & Answers
What is the sliding window pattern and when should I use it?
The sliding window pattern solves subarray or substring problems where you need to examine contiguous elements. Instead of checking every possible subarray (O(n²)), you maintain a "window" that slides left or right. Use it when the problem asks for a max/min sum, longest/shortest substring, or something involving consecutive elements. The key insight: when you move the right pointer, the window grows; when you move the left pointer, it shrinks. Most problems fall into fixed-size (window length k) or variable-size (expand until condition met, then shrink from left).
How do I find the maximum sum of any subarray of size k?
Use a fixed-size sliding window. Start with a window of the first k elements and compute its sum. Then slide: subtract the element leaving the left side, add the element entering the right side. Track the maximum sum seen. No nested loops needed—each element is added once and removed once, so it runs in O(n). In code: initialize sum for indices 0 to k-1; loop from k to n-1, do sum += arr[i] - arr[i-k], then compare sum to max.
// Max sum of subarray of size k
int MaxSumSubarray(int[] arr, int k) {
int sum = 0;
for (int i = 0; i < k; i++) sum += arr[i];
int max = sum;
for (int i = k; i < arr.Length; i++) {
sum += arr[i] - arr[i - k];
max = Math.Max(max, sum);
}
return max;
}
// Example: MaxSumSubarray(new[] { 2, 1, 5, 1, 3, 2 }, 3) → 9What is the variable-size sliding window and when do I shrink from the left?
Variable-size window has no fixed length. You expand the window by moving the right pointer until a condition is satisfied (e.g., "contains k distinct characters"). Once satisfied, you try to shrink from the left by moving the left pointer—either to find a smaller valid window or to violate the condition so you can expand again. Shrink when the current window violates a constraint (e.g., more than k distinct chars) or when you are optimizing for minimum window size.
How do I solve "longest substring with at most k distinct characters"?
Use a variable-size window with a frequency map. Start both pointers at 0. Expand right, adding each character to the map. When the map has more than k distinct characters, shrink from the left: decrement the count of the left character, remove it from the map if count hits 0, then move left forward. At each valid state (≤k distinct), update the longest length. Time O(n), space O(k) for the map.
int LongestKDistinct(string s, int k) {
var freq = new Dictionary<char, int>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.Length; right++) {
freq[s[right]] = freq.GetValueOrDefault(s[right], 0) + 1;
while (freq.Count > k) {
freq[s[left]]--;
if (freq[s[left]] == 0) freq.Remove(s[left]);
left++;
}
maxLen = Math.Max(maxLen, right - left + 1);
}
return maxLen;
}When should I use a deque instead of two pointers for sliding window?
Use a deque when you need to track the minimum or maximum in the current window efficiently. For "sliding window maximum," maintain a deque that stores indices in decreasing order of value. When you add a new element, remove from the back all elements smaller than it (they can never be the max). The front of the deque is always the max for the current window. When the front slides out of the window, pop it from the front. This gives O(n) instead of O(nk) for a naive approach.
// Sliding window maximum using deque
int[] MaxSlidingWindow(int[] nums, int k) {
var deque = new LinkedList<int>();
var result = new List<int>();
for (int i = 0; i < nums.Length; i++) {
while (deque.Count > 0 && nums[deque.Last!.Value] <= nums[i])
deque.RemoveLast();
deque.AddLast(i);
if (deque.First!.Value <= i - k) deque.RemoveFirst();
if (i >= k - 1) result.Add(nums[deque.First!.Value]);
}
return result.ToArray();
}Loading...
Related Interview Guides
- All DSA & Coding Patterns Topics
- System Design Interview Questions
- React Interview Questions
- SQL Interview Questions
- Interview Preparation — Start Here
Surya Singh
Azure Solutions Architect & AI Engineer
Microsoft-certified Azure Solutions Architect with 8+ years in enterprise software, cloud architecture, and AI/ML deployment. I build production AI systems and write about what actually works—based on shipping code, not theory.
- Microsoft Certified: Azure Solutions Architect Expert
- Built 20+ production AI/ML pipelines on Azure
- 8+ years in .NET, C#, and cloud-native architecture