Sliding Window Interview Questions
What is the Sliding Window pattern? Give a problem where you would use it — spoken sample answer for Indian interviews.
- 5Questions with answers
- 3Difficulty levels
Questions (5)
Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.
What is the Sliding Window pattern? Give a problem where you would use it.
Maintain a contiguous range [left, right] on an array or string and adjust boundaries when a constraint breaks.
Contiguous subarray or substring problems — max sum of size k, minimum window substring, longest substring without repeating characters.
Longest substring without repeating characters. Keep a map of last seen index. Expand right; if s[right] is already in the window, jump left past the old index. Complexity: Time O(n) , Space O(k) for the character map
Do not use sliding window on subsequence problems — those usually need DP or a different two-pointer setup.
How would you find the minimum window substring that contains all characters of t?
This is the hard sliding-window problem. I need a count of what t still needs. I expand right and decrement need. When need hits zero I have a valid window. Then I shrink left as much as I can while it stays valid, and I record the best. Each index moves at most twice, so O(n) if the alphabet is bounded. I would not use a nested scan from every left. I would also mention that extra characters in s are fine. Minimum window is the problem I name when they ask for a hard window.
Fixed-size versus variable-size window — when do you use each?
Fixed size is when k is given: max sum of k cards, max in a window of k. I add the new right and subtract the leaving left. Variable size is when the constraint is a property — at most k unique, sum at most S, no repeats. I expand right and shrink left until the invariant holds. I get this wrong when I try a variable window on a subsequence problem. Window means contiguous. If they say subsequence I switch tools.
What is the longest substring without repeating characters, and what is the trick with last-seen indices?
I keep a map from character to last index. I walk right. If I have seen s[right] and that index is still inside the window, I jump left to lastIndex plus one. Then I update lastIndex and the max length. Jumping left is faster than shrinking one by one when the repeat is far left. Still O(n) . The bug is forgetting to take max(left, last+1) and accidentally moving left backwards. I always mention that bug so they know I have failed it once.
Can sliding window work on a 2D grid?
Not as a literal left-right on a 2D matrix for an arbitrary region — that is usually prefix sums or DP. I have used a 1D window on each row or on a compressed column after I fix two horizontal bounds, which is the O(n squared m) trick for maximum rectangle of 1s variants. If they say 'window on a grid' I would ask whether the region must be contiguous rows and columns. I would not force a 1D window onto a shortest-path grid. That is BFS.
Practice with AI mock interviews
Run DSA mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.
Free to start · No credit card required