Notes
Coding questions I have worked through, with the pattern to remember.
SHOWING 6 OF 20 NOTES
Sort
- Newest first
- Oldest first
- Problem #, lowest first
- Problem #, highest first
- Title A–Z
- Title Z–A
- Easiest first
- Hardest first
- MediumOct 22, 2026
- string
- hash map
- sliding window
Permutation in String"does s2 contain a permutation / anagram of s1?" → fixed-size sliding window of len(s1) + letter counts. "Any order" means compare counts; "substring" means a contiguous window of a known length. When the window moves, one character comes in and one goes out: update those two counts and never recount the window. Compare with a 26-element list ==, or keep a missing counter for O(1). - MediumOct 21, 2026
- string
- hash map
- sliding window
Longest Repeating Character Replacement"longest substring that can be made uniform with ≤ k changes" → sliding window + letter counts, valid while windowlen - maxfreq <= k. More generally: turn the condition into a check that's O(1) to update as the window moves, then r adds, l removes, and you record r - l + 1. For longest problems the window can just slide (if, move l by one) instead of shrinking, and helpers like maxfreq never have to go down. - MediumOct 20, 2026
- string
- hash set
- hash map
- and sliding window
Longest Substring Without Repeating Characters"longest contiguous run where a condition holds" → sliding window. r always advances; when adding s[r] would break the condition, shrink from the left until it holds again; then record r - l + 1. Every element enters and leaves once, so the nested while is still O(n). If you can compute where l must go (here: one past the last copy of s[r]), jump there directly with a last-index map. - HardOct 19, 2026
- array
- two pointers
- prefix max
- and monotonic stack
Trapping Rain Water"how much fits above each position" → think per column: the answer at i is min(max to the left, max to the right) - height[i]. Needing "max to the left/right" for every index → prefix/suffix max arrays. And when only the smaller of the two matters, two pointers from both ends replace the arrays: move the side with the shorter bar, because the taller side guarantees a wall at least that high. - EasyOct 19, 2026
- array
- running min
- sliding window
Best Time to Buy and Sell Stock"best pair i < j" → fix the right end and keep the best left end seen so far. Here the best left end is the running minimum, so one pass replaces the O(n^2) pair scan. Write it as a sliding window [l, r]: r always advances, l jumps to r when the window stops being worth keeping (a new low). That shape is the template for every sliding-window problem. The same trick shows up as "running max/min + answer at each step" and as Kadane's algorithm on the differences. - MediumOct 17, 2026
- array
- two pointers
- greedy
Container With Most Waterbest pair where the value depends on the width (distance) and the smaller of the two → two pointers from both ends, move the limiting side. Start as wide as possible; each step gives up width, so the only hope is a taller limiting bar, so throw away the shorter one. The proof idea to remember: the shorter bar has already had its best container (the widest one), so drop it.