Learn โบ DSA Patterns
DSA Patterns
The handful of algorithm patterns that keep showing up in coding interviews. Each one has a walkthrough, a visualizer, working TypeScript, and problems to practice on.
Patterns
Pick a Pattern
Two Pointers
Converge two pointers from opposite ends of an array or string to solve palindrome, pair-sum, and comparison problems in O(N) time, O(1) space.
O(N) time ยท O(1) spaceSliding Window
Maintain a running window over arrays or strings to turn O(Nยฒ) brute force into O(N) for subarray and substring problems.
O(N) time ยท O(1)โO(K) spaceFast and Slow Pointers
Floyd's Tortoise and Hare โ two pointers moving at different speeds through a linked list to detect cycles in O(N) time, O(1) space.
O(N) time ยท O(1) spaceTop K Elements
Use a min-heap of size K to find the K largest, most frequent, or closest elements without fully sorting the input.
O(N log K) time ยท O(K) spaceIn-Place Linked List Reversal
Flip every pointer in a linked list using three variables โ prev, curr, and temp โ in one pass, no stack or array required.
O(N) time ยท O(1) spaceBinary Search
Throw away half the remaining candidates with every comparison โ find a value, a boundary, or the answer to a monotonic question in a sorted range.
O(log N) time ยท O(1) spaceWorked Examples
Case Studies
The wrong turns on real LeetCode problems, not just the answer that worked.
Case Study: Valid Palindrome (LeetCode 125)
Five bugs, caught one at a time, before the Two Pointers version finally passed. Every wrong turn is here too.
Case Study: 3Sum (LeetCode 15)
The duplicate-skipping logic is where this one bites. Four attempts, four bugs, then a clean Two Pointers solution.
Case Study: Longest Substring Without Repeating Characters (LeetCode 3)
Six bugs, mostly around when to shrink the window and how to move the left pointer. The full trail to a working Sliding Window.
Case Study: Longest Repeating Character Replacement (LeetCode 424)
The trick is knowing you never have to shrink the window. Six bugs before that clicked, all written up.
Case Study: Remove Nth Node From End of List (LeetCode 19)
Four attempts to get the gap between the two pointers right, and why the dummy node saves you from the edge cases.
Case Study: Linked List Cycle (LeetCode 141)
A short one. Two bugs, both about null checks on the fast pointer, then Floyd's Cycle Detection works.
Case Study: Substrings of Size Three With Distinct Characters (LeetCode 1876)
Not a debugging story so much as a trimming one: from an over-built first pass to a fixed-size window that allocates nothing.
Case Study: Reverse Linked List (LeetCode 206)
Four attempts at the order you reassign prev, curr, and temp. Get it wrong and you drop half the list.
Case Study: Find First and Last Position of Element in Sorted Array (LeetCode 34)
Six bugs, nearly all of them off-by-one, on the way to a two-pass binary search that finds both boundaries.
More in Learn
The rest of the Learn library covers SEO, marketing, system design, and finance.
โ Back to Learn