/Practice

Coding patterns reels

Coding patterns1 / 75
1 / 5

When you see…

Sorted array or string · Find a pair or triplet with a target sum · Reverse, partition or remove in place · Palindrome checks

The patternWalk inwards from both ends of a sorted sequence.
2 / 5

The idea

Put one pointer at each end. Compare, then move the pointer that can only improve the answer. Each step discards a whole row of possibilities, so you visit each element once.

ComplexityO(n) time after sorting, O(1) extra space
3 / 5

Worked: 3Sum

Return all unique triplets in nums that sum to zero. Sort the array. Fix one number, then run two pointers on the rest for the negated value. Skip duplicates at every level so triplets stay unique.

4 / 5

Practise next

Valid Palindrome (Easy): Skip non-alphanumerics from both ends. Container With Most Water (Medium): Always move the shorter wall. Trapping Rain Water (Hard): Track left max and right max; move the smaller side.

5 / 5

Traps

Forgetting to sort first. Infinite loop when neither pointer moves. Duplicate answers without skip logic.

Two pointers: spot it, crack it
#pattern#coding
Coding patterns2 / 75
Problem · Two pointers

3Sum

Return all unique triplets in nums that sum to zero.

function threeSum(nums: number[]): number[][] {nums.sort((a, b) => a - b);const out: number[][] = [];for (let i = 0; i < nums.length - 2; i++) {if (i > 0 && nums[i] === nums[i - 1]) continue;let lo = i + 1, hi = nums.length - 1;while (lo < hi) {const s = nums[i] + nums[lo] + nums[hi];if (s < 0) lo++;else if (s > 0) hi--;else {out.push([nums[i], nums[lo], nums[hi]]);lo++;while (lo < hi && nums[lo] === nums[lo - 1]) lo++;}}}return out;}
Step 1/5Read the problem. Which signal points to two pointers?
3Sum · quick solution
Read the problem. Which signal points to two pointers?
#two pointers#solution
Coding patterns3 / 75
Template · Two pointers

Pair with target sum (sorted)

The reusable shape behind every two pointers problem. Memorise it, then adapt the twist.

function pairSum(nums: number[], target: number): [number, number] | null {let lo = 0, hi = nums.length - 1;while (lo < hi) {const s = nums[lo] + nums[hi];if (s === target) return [lo, hi];if (s < target) lo++;else hi--;}return null;}
Step 1/3Start at both ends of the sorted array.
Two pointers template
Start at both ends of the sorted array.
#template#typescript
Container With Most Water · quick solution
Trapping Rain Water · quick solution
Sliding window: spot it, crack it
Minimum Window Substring · quick solution
Sliding window template
Longest Repeating Character Replacement · quick solution
Sliding Window Maximum · quick solution
Fast and slow pointers: spot it, crack it
Linked List Cycle II · quick solution
Fast and slow pointers template
Happy Number · quick solution
Find the Duplicate Number · quick solution
Binary search: spot it, crack it
Koko Eating Bananas · quick solution
Binary search template
Search in Rotated Sorted Array · quick solution
Capacity To Ship Packages Within D Days · quick solution
Prefix sums and hashing: spot it, crack it
Longest Consecutive Sequence · quick solution
Prefix sums and hashing template
Group Anagrams · quick solution
Product of Array Except Self · quick solution
Monotonic stack: spot it, crack it
Daily Temperatures · quick solution
Monotonic stack template
Largest Rectangle in Histogram · quick solution
Remove K Digits · quick solution
Heap / top-K: spot it, crack it
Merge K Sorted Lists · quick solution
Heap / top-K template
Top K Frequent Elements · quick solution
Find Median from Data Stream · quick solution
Intervals: spot it, crack it
Meeting Rooms II · quick solution
Intervals template
Insert Interval · quick solution
Non-overlapping Intervals · quick solution
BFS and DFS: spot it, crack it
Number of Islands · quick solution
BFS and DFS template
Rotting Oranges · quick solution
Word Ladder · quick solution
Topological sort: spot it, crack it
Course Schedule · quick solution
Topological sort template
Course Schedule II · quick solution
Alien Dictionary · quick solution
Union-find: spot it, crack it
Redundant Connection · quick solution
Union-find template
Number of Provinces · quick solution
Accounts Merge · quick solution
Backtracking: spot it, crack it
Combination Sum · quick solution
Backtracking template
Permutations · quick solution
N-Queens · quick solution
Dynamic programming: spot it, crack it
Coin Change · quick solution
Dynamic programming template
Longest Increasing Subsequence · quick solution
Edit Distance · quick solution
Trie: spot it, crack it
Search Suggestions System · quick solution
Trie template
Implement Trie · quick solution
Word Search II · quick solution
Greedy: spot it, crack it
Gas Station · quick solution
Greedy template
Jump Game II · quick solution
Partition Labels · quick solution