Sliding Window Technique Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you recognize the pattern for contiguous subarray/substring problems and can distinguish fixed-size from variable-size windows
Recomputing the entire window sum or property from scratch at each position instead of maintaining a running state
Distinguish fixed-size windows (constant k) from variable-size windows (expand/shrink on a condition) with concrete examples
Interview-ready guide
Interview answer
The sliding window technique is a way to efficiently process contiguous subarrays or substrings.
Instead of recalculating everything for each position, you maintain a running state and update it incrementally. You add the new element coming into the window and remove the element leaving the window. This turns a nested loop solution that's O of n squared into a single pass that's O of n.
There are two main types. Fixed-size windows are used when the problem gives you an exact size k, like find the maximum sum of any three consecutive elements. You compute the first window once, then slide by adding the next element and subtracting the first element of the previous window.
Variable-size windows are for problems with conditions, like longest substring with at most K distinct characters. You use two pointers. The right pointer expands to add elements.
When the condition breaks, the left pointer contracts to restore validity. You track the maximum or minimum window size you see. Recomputing every range with nested loops repeats work.
The window is useful because its state is updated incrementally as the boundaries move. You'd use this pattern whenever you see the words contiguous, consecutive, subarray, or substring combined with optimization keywords like maximum, minimum, longest, or shortest. Those signals matter only when removing the left item and adding the right item preserve enough information to update the result correctly.
Sliding Window Technique Java | Fresher Interview
Direct Answer
The sliding window technique maintains a contiguous subarray or substring that slides over the data. Instead of recomputing from scratch at each position, you add the element entering and remove the element leaving. This turns many O(n^2) brute-force solutions into O(n) for problems on contiguous subarrays or substrings.
Code Example
// Sliding window (fixed size): max sum of any k consecutive elements
public static int maxSumOfK(int[] arr, int k) {
int windowSum = 0;
for (int i = 0; i < k; i++) windowSum += arr[i]; // first window
int maxSum = windowSum;
for (int i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k]; // add entering, remove leaving
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}
// arr = {2,1,5,1,3,2}, k = 3 -> 9 (the window [5,1,3])
Variable-size problems reuse the same idea with two pointers: a right that expands and a left that contracts when the window breaks a rule.
Common Mistakes & Pitfalls
- Recomputing from scratch: writing nested loops that re-sum the next k elements for every start index is O(n^2). The whole point is incremental update — add one, remove one.
- Mixing fixed and variable templates: for "longest substring with at most 2 distinct characters" you can't slide a window of size 2 — the answer might be much longer. Use expand/contract logic.
- Off-by-one in window boundaries: be explicit about whether the window is
[left, right)or[left, right]and stick to it.
Check your understanding
Answer these without looking back at the article.
- How do you find the maximum sum subarray of size k?
- How do you find the longest substring without repeating characters?
- What is the minimum window substring problem?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.