Sliding Window And Two Pointers
Asked of: Software Engineer
Last updated

What's being tested
These problems test sliding-window and two-pointer reasoning: maintaining a valid contiguous range or exploiting sorted order without redundant work. You should demonstrate correct boundary movement, appropriate state structures, and O(n) time with O(1) or O(n) auxiliary space where applicable.
Patterns & templates
- Fixed-size sliding window — initialize the first
kelements, update by adding right and removing left; compute each result inO(n)time. - Variable-size window — expand
right, then advanceleftwhile invalid; record the best valid window after restoring the invariant. - Last-seen index map — for repeated characters or values, jump
lefttomax(left, last[value] + 1)so it never moves backward. - Monotonic deques — maintain decreasing maxima and increasing minima for limit-constrained windows; each element enters and leaves once, giving
O(n)time. - Sorted two pointers — start at both ends; if the sum is too small move
left, if too large moveright, usingO(1)extra space. - Window bounds — use inclusive indices consistently: length is
right - left + 1; remove elements only when they leave the window. - Rounding and arithmetic — retain an exact running sum, then round each average at output time; handle fewer-than-window-size inputs explicitly.
Common pitfalls
Pitfall: Moving
leftdirectly to a stale duplicate index can move it backward; always usemax(left, last_seen[value] + 1).
Pitfall: Removing from a deque or set without checking whether the outgoing index/value is still represented can corrupt window state.
Pitfall: Assuming two pointers works on an unsorted array; sorting changes original positions and may violate required index or constant-space constraints.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Find the longest subarray whose maximum and minimum differ by at most a limitGeneral · Software Engineer · Other · medium
- Longest vacation after converting at most k work days into days offGeneral · Software Engineer · Other · medium
- Flag users whose rolling-window transaction total exceeds a tiered thresholdGeneral · Software Engineer · Other · medium
- Flag transactions that repeat an amount within a time windowGeneral · Software Engineer · Other · easy
- Return the maximum of every sliding window of size kGeneral · Software Engineer · Other · hard
- Compute 3-day rolling averages of daily deposits, rounded to 2 decimalsGeneral · Software Engineer · Other · easy
- Find two numbers in a sorted array that add up to a targetGeneral · Software Engineer · Other · medium
- Check whether an array has equal values at most k indices apartGeneral · Software Engineer · Other · easy
- Maximum Number of Events in Any Half-Open Time Window of Fixed LengthGeneral · Software Engineer · Other · medium
- Find the Longest Substring without Repeated CharactersGeneral · Software Engineer · Technical Screen · medium
Related concepts
- Two Pointers, Sliding Windows And IntervalsCoding & Algorithms
- Sliding Window And Two-Pointer ScanningCoding & Algorithms
- Sliding Window Frequency MapsCoding & Algorithms
- String And Sliding Window AlgorithmsCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms
- Sliding Window And Time Window Counters