Sliding window reduces O(n²) nested loops to O(n) by maintaining a window of contiguous elements
Fixed-size windows always same length, update by subtracting left, adding right in O(1)
Dynamic windows grow right, shrink left while constraint violated; use while not if
Performance: each element added once and removed once — total O(n) time, O(1) or O(k) space
Production insight: wrong shrink logic (if vs while) causes silent data corruption in real-time analytics
Biggest mistake: using if instead of while for dynamic window shrink — leads to invalid intermediate states
Plain-English First
Imagine you're reading a very long receipt from a grocery store, and you need to find the 3 consecutive items that cost the most together. Instead of adding up every possible group of 3 from scratch, you slide a physical 'window' of 3 items across the receipt — dropping one item off the left and picking up one on the right as you go. That's it. A sliding window is just a moving frame over a sequence of data that lets you avoid redundant recalculations.
Sliding window problems show up in almost every technical interview at top-tier companies — not because they're obscure, but because they test whether you think algorithmically or just mechanically. The naive solution to most array and string problems is O(n²) nested loops. The sliding window technique collapses that to O(n), and interviewers use these problems specifically to see if you can make that leap without prompting.
The core problem it solves is this: any time you need to examine a contiguous subarray or substring under some constraint — maximum sum, longest without repeating characters, smallest with a target sum — you're repeatedly looking at overlapping data. Brute force recalculates that overlapping data every single iteration. A sliding window maintains just enough state so you only ever process each element once as the window expands and once as it contracts.
By the end of this article you'll be able to recognise which of the two main window types (fixed-size vs dynamic) applies to a given problem, implement both from memory, handle the edge cases that trip up even experienced candidates, and answer the follow-up questions interviewers use to separate good solutions from great ones.
Fixed-Size Windows — When the Frame Never Changes
A fixed-size window is the simpler of the two patterns. The window length is given to you upfront and never changes — your only job is to slide it across the array one step at a time and track whatever metric you care about.
The key insight is how you update the window in O(1) instead of O(k): when the window moves one position to the right, exactly one element leaves the left edge and one enters the right edge. That means you don't need to sum (or hash, or count) the whole window again — you just subtract the outgoing element and add the incoming one.
This pattern covers problems like: maximum average subarray of length k, maximum sum subarray of length k, count of anagram occurrences in a string, and find all substrings containing exactly k distinct characters with a fixed length.
The implementation template is always the same: build the first window, record your answer, then loop from index k to n-1, sliding by one each iteration. Get that template into muscle memory and the code almost writes itself.
MaxSumFixedWindow.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package io.thecodeforge.algorithms;
publicclassMaxSumFixedWindow {
/**
* Finds the maximum sum of any contiguous subarray of exactly 'windowSize' elements.
* Time: O(n) — each element is added once and removed once
* Space: O(1) — no extra data structures needed
*/
publicstaticintfindMaxSum(int[] prices, int windowSize) {
int totalElements = prices.length;
// Edge case: window is larger than the array itselfif (windowSize > totalElements) {
thrownewIllegalArgumentException(
"Window size " + windowSize + " exceeds array length " + totalElements
);
}
// Step 1: Build the first window by summing the first 'windowSize' elementsint currentWindowSum = 0;
for (int i = 0; i < windowSize; i++) {
currentWindowSum += prices[i];
}
// The first window is our baseline best answerint maxSum = currentWindowSum;
// Step 2: Slide the window one position at a time across the rest of the arrayfor (int rightEdge = windowSize; rightEdge < totalElements; rightEdge++) {
int leftEdge = rightEdge - windowSize; // the element we're dropping off the left// Drop the outgoing element, add the incoming element — one operation each
currentWindowSum += prices[rightEdge]; // new element entering from the right
currentWindowSum -= prices[leftEdge]; // old element leaving from the left// Update our best answer if this window beats it
maxSum = Math.max(maxSum, currentWindowSum);
}
return maxSum;
}
publicstaticvoidmain(String[] args) {
int[] dailyStockPrices = {12, 5, 35, 8, 6, 14, 21, 9, 3, 18, 27, 11, 4, 16};
int targetWindowDays = 4;
int result = findMaxSum(dailyStockPrices, targetWindowDays);
System.out.println("Stock prices: " + java.util.Arrays.toString(dailyStockPrices));
System.out.println("Max 4-day sum: " + result);
}
}
When you walk through the loop in an interview, narrate 'I'm dropping prices[leftEdge] and adding prices[rightEdge]' out loud. Interviewers specifically listen for whether you understand the subtract-then-add update — it signals you grasp WHY the window is O(n), not just that it is.
Production Insight
Fixed-size windows are used in real-time metrics like rolling 7-day averages.
In multi-threaded pipelines, the sum update must be atomic or use local copies.
Fixed window: build first, slide once, update in O(1).
The subtract-and-add update is the entire reason sliding window is O(n) instead of O(n×k).
Interviewers expect you to explain the O(1) update — practice narrating it.
Dynamic Windows — When the Frame Grows and Shrinks Based on a Condition
Dynamic (or variable-size) windows are where most candidates stumble, because the window doesn't have a fixed length — it grows until it violates a constraint, then shrinks from the left until it's valid again. The two-pointer approach drives this: a right pointer expands the window, and a left pointer contracts it.
The mental model that makes this click: think of the right pointer as greedy and optimistic — it keeps consuming elements hoping to satisfy or maximise the target. The left pointer is the enforcer — when the window breaks the rules, it evicts elements from the left until the window is valid again.
This pattern handles: longest substring without repeating characters, minimum size subarray with sum ≥ target, longest subarray with at most k distinct characters, and fruit into baskets (same idea, different flavour).
The critical implementation detail is the order of operations inside the loop: expand right first, update your state, then shrink left in a while loop until valid, then record your answer. Get that order wrong and you'll record invalid states or miss valid ones — a bug that's devilishly hard to spot under interview pressure.
LongestSubstringKDistinct.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package io.thecodeforge.algorithms;
import java.util.HashMap;
import java.util.Map;
publicclassLongestSubstringKDistinct {
/**
* Finds the length of the longest substring with at most 'maxDistinct' distinct characters.
* Time: O(n) — each pointer travels the array once
* Space: O(k) — where k is the number of distinct characters allowed
*/
publicstaticintfindLongest(String text, int maxDistinct) {
if (text == null || text.isEmpty() || maxDistinct == 0) return0;
Map<Character, Integer> charFrequencyInWindow = newHashMap<>();
int leftPointer = 0, longestFound = 0;
for (int rightPointer = 0; rightPointer < text.length(); rightPointer++) {
char incomingChar = text.charAt(rightPointer);
charFrequencyInWindow.put(incomingChar, charFrequencyInWindow.getOrDefault(incomingChar, 0) + 1);
// Shrink from the left until we're back to maxDistinct distinct charswhile (charFrequencyInWindow.size() > maxDistinct) {
char outgoingChar = text.charAt(leftPointer);
charFrequencyInWindow.put(outgoingChar, charFrequencyInWindow.get(outgoingChar) - 1);
if (charFrequencyInWindow.get(outgoingChar) == 0) {
charFrequencyInWindow.remove(outgoingChar);
}
leftPointer++;
}
longestFound = Math.max(longestFound, rightPointer - leftPointer + 1);
}
return longestFound;
}
publicstaticvoidmain(String[] args) {
String dnaSequence = "AABABCBCBAABC";
int result = findLongest(dnaSequence, 2);
System.out.println("DNA sequence: " + dnaSequence);
System.out.println("Longest valid substring length (2 distinct): " + result);
}
}
Output
DNA sequence: AABABCBCBAABC
Longest valid substring length (2 distinct): 5
Watch Out:
The most common dynamic window bug is using an if instead of a while to shrink the window. An if only evicts one character per expansion step. But adding one new character could require evicting many characters (e.g., all remaining instances of a character) before the window is valid again. Always use while to shrink — it loops until the constraint is genuinely satisfied.
Production Insight
Dynamic windows appear in streaming anomaly detection where windows grow until a deviation threshold is exceeded.
Using if instead of while can cause unbounded state growth and memory leaks.
In production, always bound the window size to prevent denial-of-service via extreme input.
Key Takeaway
Dynamic window: expand right, shrink left with while.
An if only evicts one element — a while evicts until valid.
The shrink loop must come before the answer update for maximum-length problems.
Recognising the Pattern Fast — The 3-Question Decision Framework
The hardest part of sliding window problems in an interview isn't the code — it's recognising within 60 seconds that sliding window is even the right tool. Interviewers watch this recognition moment closely.
Three questions will get you there every time. First: does the problem involve a contiguous subarray or substring? If the order doesn't matter or elements don't need to be adjacent, sliding window is the wrong tool — reach for a hash map or a sort instead. Second: is there a constraint on the window (sum ≥ target, at most k distinct, no repeating characters)? That constraint is what drives the window's expansion and contraction logic. Third: are you optimising for a minimum or maximum (shortest, longest, smallest, largest)? This tells you whether to update your answer after expanding (maximum) or after contracting (minimum).
For minimum problems — like smallest subarray with sum ≥ target — the trick is you shrink the window as aggressively as possible while still meeting the constraint, updating your best answer after each contraction, not after expansion.
MinSizeSubarraySum.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package io.thecodeforge.algorithms;
publicclassMinSizeSubarraySum {
/**
* Finds the minimum length of a contiguous subarray whose sum is >= targetSum.
* Time: O(n)
* Space: O(1)
*/
publicstaticintminSubarrayLength(int[] nums, int targetSum) {
int leftPointer = 0, currentWindowSum = 0;
int minimumLength = Integer.MAX_VALUE;
for (int rightPointer = 0; rightPointer < nums.length; rightPointer++) {
currentWindowSum += nums[rightPointer];
// Update the answer INSIDE this loop — that's the minimum-problem patternwhile (currentWindowSum >= targetSum) {
minimumLength = Math.min(minimumLength, rightPointer - leftPointer + 1);
currentWindowSum -= nums[leftPointer];
leftPointer++;
}
}
return (minimumLength == Integer.MAX_VALUE) ? 0 : minimumLength;
}
publicstaticvoidmain(String[] args) {
int[] nums = {2, 3, 1, 2, 4, 3};
int target = 7;
System.out.println("Input: " + java.util.Arrays.toString(nums) + ", Target: " + target);
System.out.println("Minimum length: " + minSubarrayLength(nums, target));
}
}
Output
Input: [2, 3, 1, 2, 4, 3], Target: 7
Minimum length: 2
Pattern Shortcut:
Maximum-length problems update the answer after the shrink phase. Minimum-length problems update the answer inside the shrink loop. This single rule separates the two flavours of dynamic window problems and stops you from hunting a subtle off-by-one for twenty minutes.
Production Insight
In real systems, you often don't have a pre-defined constraint — you're searching for the window that maximises or minimises a metric.
The 3-question framework also helps when debugging: if your algorithm seems O(n²) you likely picked the wrong pattern.
Performance rule: always confirm contiguity before committing to sliding window — non-contiguous data needs different structures.
Key Takeaway
Ask: contiguous? constraint? min or max?
Answer update location distinguishes max vs min.
This framework saves you 60 seconds of panic in every interview.
Production-Grade Implementation: Handling Stream Data
In real-world systems, data often arrives as a stream rather than a static array. For these cases, we use specialized data structures or reactive patterns. Below is how you would model a sliding window over a potentially infinite stream of integers using a standard Java queue to represent the window state.
StreamSlidingWindow.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package io.thecodeforge.algorithms;
import java.util.LinkedList;
import java.util.Queue;
/**
* Simulates a sliding window over a stream of data.
* This mimics how production systems track rolling averages or rate limits.
*/
publicclassStreamSlidingWindow {
privatefinalint windowSize;
privatefinalQueue<Integer> window = newLinkedList<>();
privatedouble runningSum = 0;
publicStreamSlidingWindow(int size) {
this.windowSize = size;
}
publicvoidaddReading(int val) {
window.add(val);
runningSum += val;
if (window.size() > windowSize) {
runningSum -= window.poll();
}
}
publicdoublegetRollingAverage() {
return window.isEmpty() ? 0 : runningSum / window.size();
}
publicstaticvoidmain(String[] args) {
StreamSlidingWindow throughputTracker = newStreamSlidingWindow(3);
int[] streamData = {100, 200, 300, 400, 500};
for (int data : streamData) {
throughputTracker.addReading(data);
System.out.println("Added: " + data + " | Current Rolling Avg: " + throughputTracker.getRollingAverage());
}
}
}
Output
Added: 100 | Current Rolling Avg: 100.0
Added: 200 | Current Rolling Avg: 150.0
Added: 300 | Current Rolling Avg: 200.0
Added: 400 | Current Rolling Avg: 300.0
Added: 500 | Current Rolling Avg: 400.0
Production Hint:
In a high-throughput Java environment, consider using a primitive Circular Buffer instead of a LinkedList for the window to avoid the overhead of object creation and garbage collection.
Production Insight
LinkedList is fine for moderate throughput, but each add/poll creates garbage.
A circular buffer with an int[] and index pointer avoids allocation entirely.
If you need multiple concurrent windows, pre-allocate all arrays to avoid GC pressure during bursts.
Key Takeaway
Streaming windows: queue or circular buffer.
Circular buffer beats LinkedList for throughput and GC.
Always consider concurrency — streaming often runs in multi-threaded pipelines.
Sliding Window Variants and Space Complexity Considerations
Sliding window can be extended to handle more complex constraints. One common variant is the monotonic queue (deque) based sliding window for problems like sliding window maximum or minimum. Instead of storing all elements, you maintain a deque of indices with monotonically decreasing values — the front always holds the maximum of the current window. This adds O(k) space but keeps the sliding process O(n).
Another variant: sliding window with hashmap for pattern matching (e.g., find all anagrams in a string). Here the window size is fixed but you need to match character frequency exactly. Use a frequency map and a counter of matched characters to avoid rescanning.
Space complexity varies across patterns. Fixed-size numeric windows can be O(1) — just two integers for sum and max. Dynamic windows with character constraints need O(k) space for frequency map, where k is the number of distinct characters allowed. Monotonic queue windows need O(k) for the deque. Understanding these trade-offs is crucial for memory-constrained environments like embedded systems or real-time trading engines.
SlidingWindowMaximum.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package io.thecodeforge.algorithms;
import java.util.ArrayDeque;
import java.util.Deque;
publicclassSlidingWindowMaximum {
publicstaticint[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0 || k <= 0) returnnewint[0];
int n = nums.length;
int[] result = newint[n - k + 1];
Deque<Integer> deque = new ArrayDeque<>(); // stores indicesfor (int i = 0; i < n; i++) {
// Remove indices outside current windowwhile (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
deque.pollFirst();
}
// Maintain decreasing order: remove smaller values from backwhile (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
deque.pollLast();
}
deque.offerLast(i);
// Record maximum when window is completeif (i >= k - 1) {
result[i - k + 1] = nums[deque.peekFirst()];
}
}
return result;
}
publicstaticvoidmain(String[] args) {
int[] nums = {1, 3, -1, -3, 5, 3, 6, 7};
int k = 3;
int[] maxes = maxSlidingWindow(nums, k);
System.out.println("Sliding window maximums: " + java.util.Arrays.toString(maxes));
}
}
Output
Sliding window maximums: [3, 3, 5, 5, 6, 7]
Space Trade-off:
Monotonic queue windows use O(k) space but avoid storing all elements — only indices that could become the maximum remain. This is optimal for sliding window maximum/minimum problems and is a common follow-up question in senior interviews.
Production Insight
Monotonic queues are essential for real-time financial tick data where you need the maximum price over the last N trades.
The deque implementation eliminates O(n*k) brute force and keeps each element added/removed at most twice.
Memory-bound systems like FPGA-accelerated tickers use circular buffers with monotonic index arrays to stay O(k) with zero heap allocation.
Key Takeaway
Deque-based sliding window for min/max preserves O(n) with O(k) space.
Space cost ranges from O(1) to O(k) — understand the constraint.
In interviews, the space follow-up tests if you know the deque trick.
● Production incidentPOST-MORTEMseverity: high
Real-Time Sensor Monitoring Pipeline Delivers Wrong Rolling Averages
Symptom
Revenue monitoring dashboard showed a sudden 15% drop in weekly average sensor values, triggering false alerts.
Assumption
The pipeline was correctly computing rolling averages using a sliding window with a queue.
Root cause
The implementation used LinkedList without synchronisation in a multi-threaded context, leading to race conditions where the queue size mismatched the running sum.
Fix
Replaced LinkedList with ConcurrentLinkedDeque and used atomic updates on the running sum via AtomicLong.
Key lesson
Always synchronize state in multi-threaded sliding window implementations.
Use thread-safe structures like ConcurrentLinkedDeque or explicit locks.
Validate sliding window behaviour with a single-threaded test harness before deploying to production streams.
Production debug guideDiagnose common sliding window implementation errors in interviews and production4 entries
Symptom · 01
Window sum does not match expected prefix sum for fixed-size window
→
Fix
Manually compute first few window sums by brute force for a small array and compare. Check if left element was correctly subtracted and right added.
Symptom · 02
Dynamic window returns incorrect maximum length
→
Fix
Isolate a test case where constraint just barely fails. Verify that the shrink loop uses while, not if, and that answer is updated after shrink (for max problems).
Symptom · 03
HashMap size never decreases in dynamic window with distinct characters
→
Fix
After decrementing a character count, immediately check if count == 0 and remove the key. The map.size() is your distinct counter — stale keys cause infinite growth.
Symptom · 04
Minimum-length problem returns 0 when valid window exists
→
Fix
Check initialisation of minimumLength: it must be Integer.MAX_VALUE, not 0. Also verify answer is updated inside the shrink while loop, not after.
Aspect
Fixed-Size Window
Dynamic Window
Window size
Given as input — constant throughout
Determined by a constraint — varies per iteration
Pointers needed
One pointer (or index + offset)
Two explicit pointers (left and right)
Shrink logic
Automatic — always drops element at index (right - k)
Conditional — while loop runs until constraint satisfied
Answer update timing
After every slide (every iteration)
Max problems: after shrink. Min problems: inside shrink loop
State tracking
Simple running total or counter
Usually a HashMap or frequency array for the window's contents
Typical problem signal
'subarray of length k' is in the problem statement
'longest', 'shortest', 'at most k', 'no repeating' — flexible length
Example problems
Max sum of k elements, count anagrams
Longest substring without repeats, min subarray sum >= target
Time complexity
O(n)
O(n) — left pointer moves at most n times total across all iterations
Key takeaways
1
The subtract-and-add update is the entire reason sliding window is O(n) instead of O(n×k)
without it you're just doing brute force with extra steps
2
Use while to shrink, never if
an if only evicts one element per expansion, but the window may need to contract multiple positions before it's valid again
3
Maximum-length problems update the answer after the shrink phase; minimum-length problems update inside the shrink loop
mix these up and you'll get off-by-one errors that are very hard to debug under pressure
4
The 3-question check (contiguous? constrained? optimising min or max?) lets you identify a sliding window problem in under 30 seconds
interviewers notice when you name the pattern before touching the keyboard
5
Space complexity varies
O(1) for numeric windows, O(k) for frequency maps, O(k) for monotonic deques — know the trade-off for the follow-up question
Common mistakes to avoid
3 patterns
×
Using if instead of while to shrink the dynamic window
Symptom
The algorithm records incorrect (invalid) window states and returns a wrong answer that passes most test cases but fails on inputs where multiple shrink steps are needed in one iteration.
Fix
Always use while (condition is violated) { shrink left; leftPointer++; } — the while loop keeps contracting until the window is genuinely valid, not just one step less violated.
×
Forgetting to remove a character from the HashMap when its frequency hits zero
Symptom
charFrequencyInWindow.size() keeps growing even when characters have left the window, so the 'distinct count' constraint is never satisfied and the left pointer races to the end of the string.
Fix
After decrementing a character's count, immediately check if (count == 0) { map.remove(character); } — the size() of the map is your distinct-character counter, and it only stays accurate if you clean up zero-count entries.
×
Initialising minimumLength as 0 instead of Integer.MAX_VALUE for minimum-size problems
Symptom
The function returns 0 even when a valid subarray exists, because 0 is always less than any real window length so Math.min() never updates it.
Fix
Always initialise to Integer.MAX_VALUE (pessimistic start), then at the end return (minimumLength == Integer.MAX_VALUE) ? 0 : minimumLength — the 0 return only happens when no valid window was ever found, which is the correct semantics.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01SENIOR
Longest Substring with at Most K Distinct Characters: Given a string s a...
Q02SENIOR
Smallest Subarray with Sum Greater than S: Given an array of positive in...
Q03SENIOR
Permutation in String: Given two strings s1 and s2, return true if s2 co...
Q04SENIOR
Longest Repeating Character Replacement: Given a string s and an integer...
Q05SENIOR
Subarrays with Product Less Than K: Given an array of integers nums and ...
Q01 of 05SENIOR
Longest Substring with at Most K Distinct Characters: Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters. (LeetCode #340)
ANSWER
Use a dynamic sliding window with a HashMap to track character frequencies. Expand right pointer, add char to map. While map.size() > k, shrink left: decrement count of left char, remove if count becomes 0, increment left. Update max length after shrink. Complexity O(n) time, O(k) space.
Q02 of 05SENIOR
Smallest Subarray with Sum Greater than S: Given an array of positive integers and a number ‘S’, find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. (LeetCode #209)
ANSWER
Dynamic sliding window. Expand right pointer adding to sum. While sum >= target, update min length, subtract left element, shrink left. Return min found or 0. O(n) time, O(1) space.
Q03 of 05SENIOR
Permutation in String: Given two strings s1 and s2, return true if s2 contains a permutation of s1. How would you apply a fixed-size window to solve this efficiently? (LeetCode #567)
ANSWER
Use fixed-size window of length s1.length(). Build frequency map for s1. Slide through s2, maintain frequency map for current window, and a match count of characters whose frequencies match exactly. When match count equals size of s1's distinct chars, return true. O(n) time, O(1) space (capped at 26 letters).
Q04 of 05SENIOR
Longest Repeating Character Replacement: Given a string s and an integer k, you can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times. Find the length of the longest substring containing the same letter. (LeetCode #424)
ANSWER
Dynamic sliding window. Expand right, update frequency of current char. Track max frequency count in window. While (window length - max frequency) > k, shrink left. Update answer after shrink. O(n) time, O(26) space.
Q05 of 05SENIOR
Subarrays with Product Less Than K: Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k. (LeetCode #713)
ANSWER
Sliding window with left pointer and running product. Expand right, multiply product. While product >= k, divide by left element and shrink left. Count subarrays ending at right: add right - left + 1 to count. O(n) time, O(1) space. Note: careful about zeros and product overflow.
01
Longest Substring with at Most K Distinct Characters: Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters. (LeetCode #340)
SENIOR
02
Smallest Subarray with Sum Greater than S: Given an array of positive integers and a number ‘S’, find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. (LeetCode #209)
SENIOR
03
Permutation in String: Given two strings s1 and s2, return true if s2 contains a permutation of s1. How would you apply a fixed-size window to solve this efficiently? (LeetCode #567)
SENIOR
04
Longest Repeating Character Replacement: Given a string s and an integer k, you can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times. Find the length of the longest substring containing the same letter. (LeetCode #424)
SENIOR
05
Subarrays with Product Less Than K: Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k. (LeetCode #713)
SENIOR
FAQ · 5 QUESTIONS
Frequently Asked Questions
01
What is the difference between a sliding window and two pointers?
Two pointers is the broader technique — it describes any algorithm using two indices that move through a data structure. Sliding window is a specific application of two pointers where the two indices define the edges of a contiguous subarray or substring and move in the same direction (both left to right). All sliding windows use two pointers, but not all two-pointer problems are sliding windows.
Was this helpful?
02
When should I use a sliding window versus a prefix sum array?
Use a sliding window when you need to find an optimal window (longest, shortest, maximum, minimum) under a dynamic constraint that changes as you scan — especially when the constraint depends on the content of the window, like distinct characters or no repeats. Use a prefix sum when you need to answer multiple arbitrary range-sum queries on a fixed array, since prefix sums let each query run in O(1) after O(n) preprocessing.
Was this helpful?
03
Why does my sliding window give wrong answers on arrays with negative numbers?
The standard dynamic sliding window assumes that adding more elements to the window can only make the sum larger (or at worst neutral), so you can always meaningfully expand right and contract left. Negative numbers break this assumption — shrinking the window might actually increase the sum, meaning the greedy expand-then-contract logic no longer guarantees you've found the optimal window. For arrays with negatives, Kadane's algorithm or a deque-based approach is usually the right tool instead.
Was this helpful?
04
How do I choose between a fixed and dynamic window in an interview?
Look at the problem statement: if it specifies an exact window length (like 'subarray of length k'), it's fixed. If it uses words like 'longest', 'shortest', 'at most', 'no more than', it's dynamic. For fixed windows, you know the size; for dynamic, you know the constraint. Also check whether the window grows and shrinks — if it never needs to shrink, it's fixed.
Was this helpful?
05
What is the space complexity of a sliding window that uses a HashMap?
It depends on the constraint. For dynamic windows with at most k distinct characters, the map stores at most k+1 entries — O(k) space. For problems like 'longest substring without repeating characters', the map stores at most the number of distinct characters in the current window, which in the worst case is O(min(n, alphabet_size)). For fixed numeric windows, O(1) space is possible with just a running sum.