~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Sliding window
Grow right, shrink left while invalid; monotonic deque for window max/min.
Notes
Recognise it when: you want the longest or shortest contiguous subarray or substring that satisfies a condition that stays monotone as the window grows.
left = 0
for right, ch in enumerate(s):
add(ch)
while invalid():
remove(s[left]); left += 1
best = max(best, right - left + 1)
- Minimum window: shrink while valid, recording the answer inside the loop.
- Fixed size k: add
a[r]and removea[r - k]. - Window max/min: a monotonic deque of indexes. Pop from the back while the new value is bigger, and pop from the front when the index leaves the window.
Gotchas: the condition must be monotone (for example, "at most K distinct" works; "exactly K" = atMost(K) - atMost(K-1)). With negative numbers, a sum window breaks, so use prefix sums instead.
13 problems
Interview roadmap
Sliding window Grow and shrink a window over a string or array.
- Basics: best sum of k in a row (fixed-size window) basics py · c++ · java easy
- Repeat artists in every k-song stretch py · c++ · java easy
- Longest Substring Without Repeating Characters py · c++ · java medium
- Longest Repeating Character Replacement py · c++ · java medium
- Minimum Window Substring py · c++ · java hard
- Sliding Window Maximum py · c++ · java hard
- Median of every window py · c++ · java hard
- Equal range search and sliding-window top K 2 levels Citadel medium
- Shortest stretch with k different values GoogleUber py · c++ · java medium
- Best Time to Buy and Sell Stock py · c++ · java easy
- Permutation in String py · c++ · java medium
- Minimum Size Subarray Sum py · c++ · java medium
- Contains Duplicate II py · c++ · java easy