Top 15 DSA Patterns for Fresher Coding Tests: Two Pointers, Sliding Window, DP & Graphs
HiddenJobMarket Team
Competitive Programming & DSA Research
📌 Key Takeaway:
Stop trying to memorize hundreds of LeetCode solutions. Learn the 15 underlying algorithmic patterns that solve 90% of coding assessment problems, complete with complexity analysis and code templates.Table of Contents
1. Why Algorithmic Patterns Beat Memorization
There are over 3,000 problems on LeetCode, but only about 15 core algorithmic design patterns. When an interviewer presents a problem with unfamiliar phrasing, recognizing whether it belongs to 'Sliding Window', 'Topological Sort', or 'Merge Intervals' reveals the exact data structure and traversal logic within 30 seconds.
2. Top 15 Patterns Cheatsheet & Complexity
Here is the complete reference table of interview coding patterns, when to use them, and benchmark problems:
| Pattern Name | When to Use / Problem Clues | Typical Time | Canonical LeetCode Problem |
|---|---|---|---|
| 1. Sliding Window | Contiguous subarrays/substrings, finding min/max window satisfying condition. | O(N) | Longest Substring Without Repeating ↗ |
| 2. Two Pointers | Sorted arrays/strings, searching pairs meeting sum target, palindrome checks. | O(N) | Two Sum II - Input Array Is Sorted ↗ |
| 3. Fast & Slow Pointers | Linked List cycles, finding middle element, circular arrays. | O(N) | Linked List Cycle ↗ |
| 4. Merge Intervals | Overlapping time intervals, calendar bookings, merging ranges. | O(N log N) | Merge Intervals ↗ |
| 5. Cyclic Sort | Arrays with numbers in a given range [1 to N], finding missing/duplicate numbers. | O(N) | Find All Numbers Disappeared in Array ↗ |
| 6. In-Place Reversal of LinkedList | Reversing sublists within linked lists without allocating new nodes. | O(N) | Reverse Linked List II ↗ |
| 7. Tree BFS / Level Order | Traversing tree level-by-level, shortest path in unweighted graphs. | O(N) | Binary Tree Level Order Traversal ↗ |
| 8. Tree DFS | Path sum problems, checking ancestor/descendant relationships. | O(N) | Path Sum II ↗ |
| 9. Two Heaps | Finding median in a continuous data stream, tracking dynamic percentiles. | O(log N) insert | Find Median from Data Stream ↗ |
| 10. Subsets & Backtracking | Generating combinations, permutations, or powersets. | O(2^N) or O(N!) | Subsets ↗ & Permutations ↗ |
| 11. Modified Binary Search | Searching in sorted rotated arrays, finding peaks, unknown bounds. | O(log N) | Search in Rotated Sorted Array ↗ |
| 12. Top K Elements | Finding K largest/smallest elements, K most frequent words. | O(N log K) | Top K Frequent Elements ↗ |
| 13. K-way Merge | Merging K sorted lists or arrays into one sorted result. | O(N log K) | Merge k Sorted Lists ↗ |
| 14. Dynamic Programming (0/1 Knapsack & LCS) | Optimal substructure, overlapping subproblems, min cost path. | O(N * W) | Coin Change ↗ & Longest Common Subsequence ↗ |
| 15. Topological Sort | Course prerequisite scheduling, compilation order, directed acyclic graph cycles. | O(V + E) | Course Schedule II ↗ |
3. Pattern 1: Sliding Window (Code & Logic)
The Sliding Window pattern reduces brute-force O(N^2) nested loops on contiguous sub-arrays down to linear O(N) by expanding the right pointer and contracting the left pointer when constraints are violated.
function lengthOfLongestSubstring(s: string): number {
const charIndexMap = new Map<string, number>();
let maxLength = 0;
let left = 0;
for (let right = 0; right < s.length; right++) {
const currentChar = s[right];
if (charIndexMap.has(currentChar) && charIndexMap.get(currentChar)! >= left) {
left = charIndexMap.get(currentChar)! + 1;
}
charIndexMap.set(currentChar, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}4. Pattern 2: Two Pointers (Code & Logic)
Used on sorted collections. By positioning one pointer at the start and another at the end, you evaluate pairs and converge inward in O(N) time with O(1) extra space.
function twoSumSorted(numbers: number[], target: number): number[] {
let left = 0;
let right = numbers.length - 1;
while (left < right) {
const sum = numbers[left] + numbers[right];
if (sum === target) {
return [left + 1, right + 1]; // 1-indexed
} else if (sum < target) {
left++;
} else {
right--;
}
}
return [];
}5. Pattern 3: Fast & Slow Pointers (Floyd's Cycle)
By moving two pointers at different speeds (Slow moves 1 step, Fast moves 2 steps), any cycle in a linked list or array will eventually cause the fast pointer to catch up to the slow pointer.
6. Dynamic Programming Patterns for Freshers
For fresher placements, DP focuses primarily on 4 core archetypes: 1D Array DP (Climbing Stairs, House Robber), 0/1 Knapsack (Coin Change, Partition Equal Subset), Longest Common Subsequence (Edit Distance), and Longest Increasing Subsequence.
🔗Official Portals & Recommended Practice Links
Frequently Asked Questions (FAQ)
Is Hard-level Dynamic Programming asked for freshers?
Rarely. Over 90% of product companies and GCCs restrict fresher coding questions to Easy and Medium problems. Hard DP is generally reserved for senior staff roles or competitive programming contests.
What is the best way to practice patterns?
Solve 5-8 problems of the SAME pattern consecutively before switching to the next pattern. This cements pattern recognition much faster than jumping randomly between data structures.
Put This Blueprint Into Action Today
Discover fresh verified openings curated directly from top company career portals.