The 14 DSA Patterns You Actually Need for Placements
Why mastering core algorithmic patterns beats grinding 500 random problems on LeetCode.
StudyVex Editorial Team
Engineering & Placement Mentors
1. The Random Problem Trap
Every year during placement season, college campuses see the same phenomenon: students grind 400 or 500 disconnected LeetCode problems, yet freeze when an interviewer asks a slight twist on an array problem.
Why does this happen?
Because solving problems by memorization fails the moment an interviewer modifies the constraints. Interviewers at Google, Amazon, Microsoft, and high-growth startups do not ask textbook questions; they assess your ability to identify structural invariants and apply an algorithmic archetype.
When you classify problems by their underlying pattern, a list of 500 problems shrinks down to just 14 core patterns.
2. Pattern 1: Two Pointers (Opposite & Same Direction)
The Two Pointers pattern utilizes two memory references to iterate across an array or sequence, typically moving toward each other or in lockstep.
When to apply:
- The input array is sorted (or can be sorted without violating time constraints).
- You are looking for pairs, triplets, or sub-ranges that meet a specific target sum.
- In-place reversal or partitioning is required with $O(1)$ extra memory.
Classic Archetype: Pair with Target Sum
Given a sorted integer array nums and an integer target, find two indices such that their sum equals target.
| 1 | // C++20 Optimal Two Pointers Implementation |
| 2 | #include <vector> |
| 3 | #include <utility> |
| 4 | |
| 5 | std::pair<int, int> findTargetPair(const std::vector<int>& nums, int target) { |
| 6 | int left = 0; |
| 7 | int right = static_cast<int>(nums.size()) - 1; |
| 8 | |
| 9 | while (left < right) { |
| 10 | int currentSum = nums[left] + nums[right]; |
| 11 | if (currentSum == target) { |
| 12 | return {left, right}; // Zero-indexed pair found in O(N) time |
| 13 | } else if (currentSum < target) { |
| 14 | left++; // Need a larger sum, move left pointer rightward |
| 15 | } else { |
| 16 | right--; // Need a smaller sum, move right pointer leftward |
| 17 | } |
| 18 | } |
| 19 | return {-1, -1}; |
| 20 | } |
Complexity: Time $O(N)$, Auxiliary Space $O(1)$.
3. Pattern 2: Fast & Dynamic Sliding Window
The Sliding Window pattern maintains a valid subsegment (a "window") across contiguous elements of an array or string. It expands the right edge until constraints are violated, then shrinks the left edge to restore validity.
High-Frequency Interview Problems:
- Longest Substring Without Repeating Characters
- Minimum Size Subarray Sum
- Maximum Consecutive Ones with At Most $K$ Flips
4. Pattern 3: Modified Binary Search & Space Search
Binary search isn't just for arr[mid] == target. In technical interviews, over 70% of binary search problems operate on Search Spaces with Monotonic Invariants (e.g., Koko Eating Bananas, Capacity to Ship Packages Within D Days, Aggressive Cows).
The Invariant Principle:
Always maintain a clean invariant on your search boundaries [low, high]. At every iteration:
- Compute
mid = low + (high - low) / 2to guard against integer overflow. - Check a feasibility function
isValid(mid). - Shrink the interval deterministically.
5. The 14-Pattern Placement Matrix
| # | Pattern | Key Trigger Words | Classic Problem |
|---|---|---|---|
| 1 | Two Pointers | Sorted array, pair sum, palindromes | 3Sum, Container With Most Water |
| 2 | Sliding Window | Contiguous subarray, min/max length with condition | Longest Substring Without Repeating |
| 3 | Fast & Slow Pointers | Cycle detection, middle of sequence | Linked List Cycle, Happy Number |
| 4 | Modified Binary Search | Sorted rotated array, min capacity, monotonic range | Search in Rotated Sorted Array |
| 5 | Prefix Sum / Hash | Subarray sum equals K, equilibrium point | Subarray Sum Equals K |
| 6 | Monotonic Stack | Next greater element, histogram areas | Daily Temperatures, Largest Rectangle |
| 7 | Topological Sort | Course prerequisites, build order, DAG dependencies | Course Schedule I & II |
| 8 | Breadth-First Search | Shortest path in unweighted graphs, level-order | Word Ladder, Rotting Oranges |
| 9 | Depth-First Search | Connected components, tree traversals, backtracks | Number of Islands, Word Search |
| 10 | Subsets & Permutations | Generate combinations, state choices | Subsets II, N-Queens |
| 11 | Two Heaps | Running median, balancing highest/lowest halves | Find Median from Data Stream |
| 12 | K-way Merge | Merge sorted lists/arrays | Merge K Sorted Lists |
| 13 | 0/1 & Unbounded Knapsack | Subset choices with capacity weights | Coin Change, Partition Equal Subset Sum |
| 14 | Intervals & Merging | Overlapping meetings, calendar scheduling | Merge Intervals, Non-overlapping Intervals |
6. Your 30-Day Pattern Action Plan
- Week 1 (Days 1–7): Master Two Pointers, Sliding Window, and Prefix Sums.
- Week 2 (Days 8–14): Fast & Slow Pointers, Monotonic Stack, and Modified Binary Search.
- Week 3 (Days 15–21): Trees, BFS/DFS Graphs, and Topological Sort.
- Week 4 (Days 22–30): Dynamic Programming (Knapsack & Subsequences) and Intervals.