DSA Notes
Master the 7 most important coding patterns: sliding window, two pointers, fast & slow pointers, merge intervals, cyclic sort, top-K elements, and modified binary search.
Why Patterns Matter
Most interview problems are variations of 10-15 core patterns. Once you recognize the pattern, the solution structure becomes clear — you just need to adapt the template. This is why experienced interviewees solve problems faster: they are not inventing solutions from scratch, they are pattern-matching.
Pattern 1: Sliding Window
When to use: Problems involving contiguous subarrays/substrings with a constraint (maximum sum, minimum length, at most K distinct characters).
Key signal words: "subarray," "substring," "contiguous," "window," "consecutive"
Template (Variable-Size Window):
Example: Longest Substring Without Repeating Characters
Pattern 2: Two Pointers
When to use: Sorted arrays where you need to find pairs/triplets satisfying a condition, or partitioning problems.
Key signal: Sorted input, pair finding, in-place array manipulation.
Template (Pair Sum):
Example: 3Sum (Find Triplets That Sum to Zero)
Pattern 3: Fast & Slow Pointers (Floyd's Cycle Detection)
When to use: Linked list cycle detection, finding the middle of a list, or finding the start of a cycle.
Key insight: The slow pointer moves 1 step, fast moves 2 steps. If there is a cycle, they WILL meet. If no cycle, fast reaches the end.
Template:
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
def find_cycle_start(head):
slow = fast = head
# Phase 1: Detect cycle
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
else:
return None # No cycle
# Phase 2: Find start
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slowExample: Find the Duplicate Number (Array as Linked List)
Pattern 4: Merge Intervals
When to use: Problems involving overlapping intervals, scheduling, or meeting rooms.
Key signal: Given a list of intervals, merge/count/find gaps.
Template:
Example: Meeting Rooms II (Minimum Rooms Needed)
Pattern 5: Cyclic Sort
When to use: Problems involving arrays with numbers in range [1, n] or [0, n] where you need to find missing/duplicate numbers.
Key insight: Place each number at its "correct" index (value i at index i-1). After one pass, any misplaced number reveals the answer.
Template:
Example: Find All Missing Numbers
Pattern 6: Top-K Elements
When to use: Finding the K largest/smallest/most frequent elements.
Key insight: Use a heap of size K. For K largest, use a min-heap (root is the Kth largest). For K smallest, use a max-heap.
Template:
Example: K Closest Points to Origin
Pattern 7: Modified Binary Search
When to use: Sorted/rotated arrays, or when searching in a space where you can eliminate half the candidates each step.
Key variations: Search in rotated array, find first/last occurrence, search in matrix.
Template (Find First True):
def binary_search_first_true(lo, hi, condition):
while lo < hi:
mid = (lo + hi) // 2
if condition(mid):
hi = mid # mid could be answer, search left
else:
lo = mid + 1 # mid is too small
return loExample: Search in Rotated Sorted Array
Pattern Recognition Cheatsheet
| Problem Signal | Pattern |
|---|---|
| "Longest/shortest subarray with condition" | Sliding Window |
| "Pair in sorted array" | Two Pointers |
| "Cycle in linked list" | Fast & Slow |
| "Overlapping intervals" | Merge Intervals |
| "Missing/duplicate in [1,n]" | Cyclic Sort |
| "Kth largest/smallest" | Top-K (Heap) |
| "Sorted array search" | Modified Binary Search |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Essential Coding Patterns for Interviews.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, interview, preparation, coding
Related Data Structures & Algorithms Topics