Coding interview patterns are the key to cracking technical interviews at top tech companies. In this guide, youβll quickly review 8 essential ones in 5 minutes.
Summary:
This blog is your 5-minute last-minute revision sheet covering 8 essential coding patterns that consistently appear in top tech interviews. From two pointers to topological sort, this is the ultimate quick review before your next technical round.
Table of Contents
π§ Introduction to Coding Interview Patterns
Before walking into a technical interview, itβs crucial to quickly revise common problem-solving patterns. These 8 coding interview patterns form the backbone of FAANG-style DSA questions and are a must-know for any serious candidate.
This is nothing but quick 5 min revision before going to interview.
1. Two Pointers β A Core Coding Interview Pattern

Core Concept:
- Use two indices (pointers) to scan through a data structure (usually an array or linked list).
- Move pointers inward based on the problem condition.
- Common setup:
left = 0
right = len(arr) - 1
π When to Use:
- Array is sorted
- Looking for pairs or triplets
- In-place operations
- Merging or comparing values
β Common Use Cases:
Problem Type | Description |
---|---|
Pair Sum / Target Sum | Move inward based on sum comparison |
Palindrome Check | Compare characters from both ends |
Container With Most Water | Maximize area between two heights |
Remove Duplicates | Use slow/fast pointers |
Sorted Squares | Insert squares from ends to result |
π Strategy Variants:
- Start-End Pointers (e.g.,
left
,right
) - Slow-Fast Pointers (used in cycle detection, linked lists)
- Multiple arrays/pointers (e.g., merging two arrays)
β οΈ Pitfalls to Avoid:
- Forgetting to update pointers β infinite loop
- Not checking
left <= right
condition - Wrong direction of movement based on logic
π§ͺ Practice Questions
Level: Easy β Good for Top Interview Prep
- Two Sum II β Input array is sorted
- Valid Palindrome
- Move Zeroes
π 2. Modified Binary Search
π§ Core Concept:
Apply binary search on non-traditional conditions like rotated arrays, peaks, or specific occurrences.
π When to Use:
- The array is not fully sorted, but follows some structure (rotated, bitonic, etc.)
- Need to find first/last occurrence
- Binary logic can reduce search space
β Common Use Cases:
Problem Type | Description |
---|---|
Search in Rotated Array | Adjust mid pointer by comparing with ends |
Peak Index in Mountain Array | Find local maxima using mid comparisons |
First/Last Occurrence | Adjust search range based on target comparison |
β οΈ Pitfalls to Avoid:
- Off-by-one errors (mid calculation)
- Not updating search boundaries correctly
- Forgetting to check sorted halves
π§ͺ Practice Questions:
- Search in Rotated Sorted Array
- Peak Index in a Mountain Array
- First Bad Version
π 3. Sliding Window
π§ Core Concept:
Maintain a fixed-size or dynamic-sized window and slide it over data to track a subarray/substring.
π When to Use:
- Find max/min/average over a window
- Longest/shortest subarray with condition
- Character frequency in a substring
β Common Use Cases:
Problem Type | Description |
---|---|
Max Sum Subarray | Fixed-size window to track max sum |
Longest Substring w/o Repeat | Expand window until duplicate, then shrink |
Min Window Substring | Grow-shrink window until condition is met |
β οΈ Pitfalls to Avoid:
- Not shrinking window correctly
- Mismanaging frequency maps
- Missing edge cases with empty or full windows
π§ͺ Practice Questions:
- Maximum Average Subarray I
- Longest Substring Without Repeating Characters
- Minimum Window Substring
π 4. Fast and Slow Pointers (Cycle Detection)
π§ Core Concept:
Move two pointers at different speeds to detect cycles, midpoints, or intersection points in linked structures.
π When to Use:
- Cycle detection in linked lists
- Finding middle node
- Detecting intersection point of two linked lists
β Common Use Cases:
Problem Type | Description |
---|---|
Detect Cycle | Slow = 1 step, Fast = 2 steps |
Find Cycle Start | Reuse slow pointer logic |
Find Middle of List | Fast reaches end while slow reaches middle |
β οΈ Pitfalls to Avoid:
- Fast pointer may become nil β check before accessing
fast.next
- Infinite loop if cycle isnβt handled properly
π§ͺ Practice Questions:
- Linked List Cycle
- Find the Duplicate Number
- Middle of the Linked List
π§± 5. Prefix Sum
π§ Core Concept:
Precompute running sums to quickly answer range-based or cumulative queries.
π When to Use:
- Frequent range sum or difference queries
- Count of subarrays with a specific property (e.g., sum = K)
β Common Use Cases:
Problem Type | Description |
---|---|
Subarray Sum Equals K | Use hash map with prefix counts |
Range Sum Query | Precompute prefix sums |
Max Subarray Sum | Kadaneβs algorithm uses running sum |
β οΈ Pitfalls to Avoid:
- Off-by-one errors with indices
- Not initializing prefix map correctly
π§ͺ Practice Questions:
- Subarray Sum Equals K
- Range Sum Query – Immutable
- Continuous Subarray Sum
π 6. Two Heaps (Median of Stream)
π§ Core Concept:
Use two heaps (min-heap and max-heap) to keep track of dynamic median or balanced partitions.
π When to Use:
- Need to find median in a stream
- Need to maintain order statistics dynamically
β Common Use Cases:
Problem Type | Description |
---|---|
Find Median from Stream | Balance numbers in two heaps |
Sliding Window Median | Insert/delete while maintaining balance |
β οΈ Pitfalls to Avoid:
- Forgetting to rebalance heaps
- Not handling even vs odd size correctly
π§ͺ Practice Questions:
- Find Median from Data Stream
- Sliding Window Median
π 7. Merge Intervals
π§ Core Concept:
Sort intervals and merge overlapping ones based on comparison of end/start times.
π When to Use:
- Intervals are involved (time ranges, ranges of numbers)
- Need to find union, intersection, or gaps
β Common Use Cases:
Problem Type | Description |
---|---|
Merge Intervals | Merge based on overlapping conditions |
Insert Interval | Add and merge in-place |
Meeting Rooms | Count overlaps to schedule rooms |
β οΈ Pitfalls to Avoid:
- Not sorting by start time
- Overlapping condition not handled properly
π§ͺ Practice Questions:
- Merge Intervals
- Insert Interval
- Meeting Rooms II
π 8. Monotonic Stack
π§ Core Concept:
Use a stack to maintain monotonically increasing or decreasing order, useful for next greater/smaller element problems.
π When to Use:
- Find Next Greater Element
- Handle Histogram / Span problems
- Backtracking of previous greater/smaller
β Common Use Cases:
Problem Type | Description |
---|---|
Next Greater Element | Track decreasing order with stack |
Daily Temperatures | Use stack to find warmer future day |
Largest Rectangle in Histogram | Track heights and widths via stack |
β οΈ Pitfalls to Avoid:
- Not handling empty stack checks
- Infinite loop if elements not popped correctly
π§ͺ Practice Questions:
- Next Greater Element I/II
- Daily Temperatures
- Largest Rectangle in Histogram
Hereβs a quick recap of each pattern with sample problems:
Pattern | Sample Questions |
---|---|
Two Pointers | Valid Palindrome, Two Sum II |
Binary Search | Peak Index, Rotated Sorted Array |
BFS | Level Order Traversal, Min Depth |
DFS | Path Sum, Diameter of Tree |
Topological Sort | Course Schedule |
Top K Elements | Kth Largest, Top K Frequent |
Subset | Subsets, Combination Sum |
Sliding Window | Longest Unique Substring, Max Sum Window |
π― Conclusion
These 8 patterns give you a framework to think during interviews. Donβt just memorize solutionsβpractice these patterns using top problems from Leetcode, GeeksforGeeks, or Code & Algo.
- “These coding interview patterns are commonly asked in Google, Meta, and Amazon interviews.”
- “Mastering coding interview patterns is key to solving unseen problems quickly.”
- “Youβll see these coding interview patterns repeated in platforms like LeetCode and HackerRank.”
- “By practicing these coding interview patterns, you build intuition over time.”
- “Donβt just memorize problemsβlearn the patterns behind each solution.”
π Explore More
For additional practice problems, check out LeetCode Explore.
π Visit CodeAndAlgo.com for more system design breakdowns, coding pattern guides, and interview strategies.