~/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.
Main path: 8 problems, about 3 hours.
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.
17 problems
Interview roadmap
Sliding window Grow and shrink a window over a string or array.
- Play first: The Dragon Market 1-minute game One-pass max profit
- Best sum of k in a row basics py · c++ · java easy
- Repeat artists in every k-song stretch py · c++ · java easy
- Best Time to Buy and Sell Stock code the boss py · c++ · java easy
- Minimum Size Subarray Sum py · c++ · java medium
- Longest Substring Without Repeating Characters ▶ boss game py · c++ · java medium
- Longest Repeating Character Replacement py · c++ · java medium
- Minimum Window Substring py · c++ · java hard
- Sliding Window Maximum ▶ boss game py · c++ · java hard
More practice: 9 problems
- Contains Duplicate II py · c++ · java easy
- Repeated DNA Sequences py · c++ · java medium
- Shortest stretch with k different values GoogleUber py · c++ · java medium
- Max Consecutive Ones III py · c++ · java medium
- Fruit Into Baskets py · c++ · java medium
- Permutation in String py · c++ · java medium
- Find All Anagrams in a String py · c++ · java medium
- Median of every window py · c++ · java hard
- Equal range search and sliding-window top K assessment 2 levels Citadel py · c++ · java medium