Algorithms20 lessons13 quiz questions
DSA Coding Patterns
Master the 20 most important coding interview patterns. Each pattern includes template code in Python and TypeScript, LeetCode problems, and interview strategies.
What You Will Learn
- ✓Two Pointers
- ✓Sliding Window
- ✓Binary Search
- ✓BFS (Breadth-First Search)
- ✓DFS (Depth-First Search)
- ✓Dynamic Programming
- ✓Greedy Algorithms
- ✓Backtracking
- ✓Monotonic Stack
- ✓Heap / Top-K
- ✓Union-Find
- ✓Trie
Overview
Master the 20 most important coding interview patterns. Each pattern includes template code in Python and TypeScript, LeetCode problems, and interview strategies.
Understanding Two Pointers
The two-pointer technique uses two indices to traverse a data structure, reducing time complexity from O(n²) to O(n). It works best on sorted arrays or linked lists.
Variants:
Opposite ends: left starts at 0, right at end — move inward (Two Sum, Container With Most Water)
Same direction: slow/fast pointers — detect cycles, find middle (Floyd's algorithm)
Sliding window: left/right expand and contract — substring/subarray problems
When to use two pointers:
"Find a pair that sums to X" → Opposite ends on sorted array
"Remove duplicates in-place" → Slow (write) and fast (read) pointers
"Is palindrome?" → Compare from both ends
"Merge two sorted arrays" → One pointer per array
Two Pointers Pattern
The Two Pointers technique uses two references to traverse a data structure, often from opposite ends or at different speeds.
When to Recognize
Signal words: "sorted array", "pair that sums to", "remove duplicates", "palindrome"
The input is sorted or can be sorted
You need to find pairs or compare elements
Template: Opposite Direction
Template: Same Direction (Fast/Slow)
LeetCode Problems
#1 Two Sum (Easy) — hash map approach, but sorted variant uses two pointers
#15 3Sum (Medium) — fix one element, two-pointer for remaining
#11 Container With Most Water (Medium) — move the shorter side inward
#26 Remove Duplicates from Sorted Array (Easy)
#125 Valid Palindrome (Easy)
Walkthrough: Container With Most Water (#11)
Always move the pointer with the SHORTER height — moving the taller one can never increase the area.
Time/Space: O(n) time, O(1) space
Common Mistakes
Forgetting the array must be sorted for the opposite-direction approach
Off-by-one errors with vs
Not handling duplicates (3Sum: skip duplicate values after finding a match)
Interview Tip
"When I see a sorted array with a pair-finding problem, my first instinct is two pointers from both ends. It gives O(n) time with O(1) space, which is optimal."
Java Implementation
Sample Quiz Questions
1. What pattern would you use for 'find the longest substring without repeating characters'?
Understand·Difficulty: 2/5
2. What is the time complexity of binary search?
Remember·Difficulty: 1/5
3. When should you use BFS over DFS?
Apply·Difficulty: 3/5
+ 10 more questions available in the full app.
Related Topics
Master DSA Coding Patterns for Your Next Interview
Get access to full lessons, adaptive quizzes, cheat sheets, code playground, and progress tracking — completely free.