Mid-level 4 min · March 06, 2026

Sliding Window — LinkedList Race Condition in Averages

Unsynchronized LinkedList in multi-threaded sliding window caused 15% false alert drop.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • 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;

public class MaxSumFixedWindow {

    /**
     * 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
     */
    public static int findMaxSum(int[] prices, int windowSize) {
        int totalElements = prices.length;

        // Edge case: window is larger than the array itself
        if (windowSize > totalElements) {
            throw new IllegalArgumentException(
                "Window size " + windowSize + " exceeds array length " + totalElements
            );
        }

        // Step 1: Build the first window by summing the first 'windowSize' elements
        int currentWindowSum = 0;
        for (int i = 0; i < windowSize; i++) {
            currentWindowSum += prices[i];
        }

        // The first window is our baseline best answer
        int maxSum = currentWindowSum;

        // Step 2: Slide the window one position at a time across the rest of the array
        for (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;
    }

    public static void main(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);
    }
}
Output
Stock prices: [12, 5, 35, 8, 6, 14, 21, 9, 3, 18, 27, 11, 4, 16]
Max 4-day sum: 57
Interview Gold:
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.
Always validate edge cases: window size = 1, window size = array length, array with negative values.
Key Takeaway
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;

public class LongestSubstringKDistinct {

    /**
     * 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
     */
    public static int findLongest(String text, int maxDistinct) {
        if (text == null || text.isEmpty() || maxDistinct == 0) return 0;

        Map<Character, Integer> charFrequencyInWindow = new HashMap<>();
        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 chars
            while (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;
    }

    public static void main(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;

public class MinSizeSubarraySum {

    /**
     * Finds the minimum length of a contiguous subarray whose sum is >= targetSum.
     * Time:  O(n)
     * Space: O(1)
     */
    public static int minSubarrayLength(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 pattern
            while (currentWindowSum >= targetSum) {
                minimumLength = Math.min(minimumLength, rightPointer - leftPointer + 1);
                currentWindowSum -= nums[leftPointer];
                leftPointer++;
            }
        }

        return (minimumLength == Integer.MAX_VALUE) ? 0 : minimumLength;
    }

    public static void main(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.
 */
public class StreamSlidingWindow {
    private final int windowSize;
    private final Queue<Integer> window = new LinkedList<>();
    private double runningSum = 0;

    public StreamSlidingWindow(int size) {
        this.windowSize = size;
    }

    public void addReading(int val) {
        window.add(val);
        runningSum += val;

        if (window.size() > windowSize) {
            runningSum -= window.poll();
        }
    }

    public double getRollingAverage() {
        return window.isEmpty() ? 0 : runningSum / window.size();
    }

    public static void main(String[] args) {
        StreamSlidingWindow throughputTracker = new StreamSlidingWindow(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;

public class SlidingWindowMaximum {

    public static int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0) return new int[0];
        int n = nums.length;
        int[] result = new int[n - k + 1];
        Deque<Integer> deque = new ArrayDeque<>(); // stores indices

        for (int i = 0; i < n; i++) {
            // Remove indices outside current window
            while (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
                deque.pollFirst();
            }
            // Maintain decreasing order: remove smaller values from back
            while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
                deque.pollLast();
            }
            deque.offerLast(i);

            // Record maximum when window is complete
            if (i >= k - 1) {
                result[i - k + 1] = nums[deque.peekFirst()];
            }
        }
        return result;
    }

    public static void main(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.
AspectFixed-Size WindowDynamic Window
Window sizeGiven as input — constant throughoutDetermined by a constraint — varies per iteration
Pointers neededOne pointer (or index + offset)Two explicit pointers (left and right)
Shrink logicAutomatic — always drops element at index (right - k)Conditional — while loop runs until constraint satisfied
Answer update timingAfter every slide (every iteration)Max problems: after shrink. Min problems: inside shrink loop
State trackingSimple running total or counterUsually 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 problemsMax sum of k elements, count anagramsLongest substring without repeats, min subarray sum >= target
Time complexityO(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.
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
What is the difference between a sliding window and two pointers?
02
When should I use a sliding window versus a prefix sum array?
03
Why does my sliding window give wrong answers on arrays with negative numbers?
04
How do I choose between a fixed and dynamic window in an interview?
05
What is the space complexity of a sliding window that uses a HashMap?
🔥

That's Coding Patterns. Mark it forged?

4 min read · try the examples if you haven't

Previous
Top 10 Linked List Problems
7 / 17 · Coding Patterns
Next
Two Pointer Interview Problems