Top 50 Most Common DSA Interview Questions & Patterns
Summary: This guide focuses on patterns (sliding window, two pointers, hashing, stacks, binary search, trees, graphs, heaps/greedy, DP) and maps them to the 50 most common DSA interview questions. You’ll get Big-O hints, compact code snippets, an evaluation rubric, and a 7-day plan to convert studying into results.
Key takeaways
- Think in patterns, not problems. Say your plan before coding.
- Always state time/space complexity and call out edge cases.
- Practice under a time box and narrate trade-offs as you work.
- Use a rubric (below) to simulate a real interview loop.
Patterns Cheat Sheet
- Two Pointers / Sliding Window → subarray/substring, dedup, partitioning, min/max window
- Binary Search → sorted arrays, monotonic answers, answer-space search
- Hashing → lookups, frequency maps, de-duplication, prefix sums
- Stack / Monotonic Stack → NGE, parentheses, histogram
- Heap / Greedy → k-largest, scheduling, intervals
- Graph (BFS/DFS) → connectivity, shortest path (unweighted → BFS)
- Tree → DFS traversals + invariants
- DP → overlapping subproblems, optimal substructure
- Backtracking → combinations, permutations, subsets
- Bit/Math → parity, unique elements, gcd/lcm
1) Arrays & Two Pointers / Sliding Window
- Two Sum (hash) — O(n), O(n)
- Best Time to Buy & Sell Stock (I) — O(n), O(1)
- Maximum Subarray (Kadane) — O(n), O(1)
- Product of Array Except Self — O(n), O(1)
- Contains Duplicate (set) — O(n), O(n)
- Valid Anagram — O(n), O(Σ)
- Longest Substring Without Repeating — O(n), O(Σ)
- Minimum Window Substring — O(n), O(Σ)
- 3Sum (sort+two pointers) — O(n²), O(1)
- Container With Most Water — O(n), O(1)
- Dutch National Flag — O(n), O(1)
- Merge Intervals — O(n log n), O(n)
Example — Longest Substring Without Repeating
function lengthOfLongestSubstring(s: string): number {
const seen = new Map<string, number>();
let left = 0, ans = 0;
for (let right = 0; right < s.length; right++) {
const ch = s[right];
if (seen.has(ch) && seen.get(ch)! >= left) left = seen.get(ch)! + 1;
seen.set(ch, right);
ans = Math.max(ans, right - left + 1);
}
return ans;
}
// Time: O(n) | Space: O(min(n, Σ))Practice these now on MockQube
2) Stack & Monotonic Stack
- Valid Parentheses — O(n), O(n)
- Min Stack — O(1) ops
- Daily Temperatures — O(n), O(n)
- Next Greater Element — O(n), O(n)
- Largest Rectangle in Histogram — O(n), O(n)
- Evaluate Reverse Polish Notation — O(n), O(n)
Example — Valid Parentheses
function isValid(s: string): boolean {
const st: string[] = [];
const pair: Record<string,string> = {")":"(","]":"[","}":"{"};
for (const ch of s) {
if (ch in pair) {
if (st.pop() !== pair[ch]) return false;
} else {
st.push(ch);
}
}
return st.length === 0;
}3) Binary Search & Order Statistics
- Binary Search — O(log n)
- Search in Rotated Sorted Array — O(log n)
- Find Kth Smallest/Largest (Quickselect) — avg O(n)
- Answer-space BS (Koko/Ship Capacity) — O(n log A)
- Median of Two Sorted Arrays — O(log(min(m,n)))
Example — Answer-space Binary Search
function feasible(mid: number, arr: number[], h: number): boolean {
let hours = 0;
for (const x of arr) hours += Math.ceil(x / mid);
return hours <= h;
}
function minEatingSpeed(piles: number[], h: number): number {
let lo = 1, hi = Math.max(...piles);
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
feasible(mid, piles, h) ? hi = mid : lo = mid + 1;
}
return lo;
}4) Hashing & Prefix
- Subarray Sum Equals K — O(n), O(n)
- Longest Consecutive Sequence — O(n), O(n)
- Group Anagrams — O(n·k log k) or O(n·k)
- Two Sum II (sorted) — O(n), O(1)
5) Trees (BST/BT) — DFS/BFS
- Level Order (BFS) — O(n), O(n)
- Max Depth (DFS) — O(n)
- Validate BST (bounds) — O(n)
- LCA (BST/BT) — O(h)/O(n)
- Diameter — O(n)
- Serialize/Deserialize — O(n)
Example — Validate BST
type N = { val: number; left: N|null; right: N|null } | null;
function isValidBST(root: N, low: number = -Infinity, high: number = Infinity): boolean {
if (!root) return true;
if (root.val <= low || root.val >= high) return false;
return isValidBST(root.left, low, root.val) && isValidBST(root.right, root.val, high);
}6) Graphs — BFS/DFS/Toposort/Union-Find
- Number of Islands — O(mn)
- Clone Graph — O(V+E)
- Course Schedule (Kahn) — O(V+E)
- Rotting Oranges — O(mn)
- Pacific Atlantic — O(mn)
- Accounts Merge (Union-Find) — ~O(n)
Example — Course Schedule (Kahn)
function canFinish(n: number, prereq: number[][]): boolean {
const indeg = Array(n).fill(0);
const g: number[][] = Array.from({length:n},()=>[]);
for (const [a,b] of prereq) { g[b].push(a); indeg[a]++; }
const q: number[] = [];
for (let i=0;i<n;i++) if (indeg[i]===0) q.push(i);
let seen = 0;
while (q.length) {
const u = q.shift()!;
seen++;
for (const v of g[u]) if (--indeg[v]===0) q.push(v);
}
return seen === n;
}7) Heaps / Greedy / Intervals
- Kth Largest — heap O(n log k)
- Top K Frequent — heap/bucket O(n log k)/O(n)
- Task Scheduler — O(n)
- Meeting Rooms II — O(n log n)
- Merge K Sorted Lists — O(n log k)
Example — Min-Heap sketch
class MinHeap {
private a: number[] = [];
private up(i:number){while(i>0){const p=(i-1)>>1;if(this.a[p]<=this.a[i])break;[this.a[p],this.a[i]]=[this.a[i],this.a[p]];i=p;}}
private down(i:number){const n=this.a.length;while(true){let s=i,l=i*2+1,r=l+1;if(l<n&&this.a[l]<this.a[s])s=l;if(r<n&&this.a[r]<this.a[s])s=r;if(s===i)break;[this.a[s],this.a[i]]=[this.a[i],this.a[s]];i=s;}}
push(x:number){this.a.push(x);this.up(this.a.length-1);}
pop(){const n=this.a.length;[this.a[0],this.a[n-1]]=[this.a[n-1],this.a[0]];const v=this.a.pop();this.down(0);return v;}
top(){return this.a[0];}
size(){return this.a.length;}
}
function findKthLargest(nums:number[], k:number): number {
const h=new MinHeap();
for(const x of nums){h.push(x); if(h.size()>k) h.pop();}
return h.top();
}8) Dynamic Programming
- Climbing Stairs — O(n), O(1)
- House Robber I/II — O(n), O(1)
- Coin Change — O(amount·n)
- Longest Increasing Subsequence — O(n log n)
- Edit Distance — O(mn)
- 0/1 Knapsack — O(nW)
Example — Coin Change
function coinChange(coins: number[], amount: number): number {
const dp = Array(amount+1).fill(Infinity);
dp[0] = 0;
for (const c of coins) {
for (let a = c; a <= amount; a++) {
dp[a] = Math.min(dp[a], dp[a - c] + 1);
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}Evaluation Rubric (what interviewers look for)
- Pattern ID: Map problem to a known pattern.
- Complexity: State time/space + trade-offs.
- Edge cases: Empty, duplicates, negatives, large sizes.
- Testing: 3+ cases before/after coding.
- Clarity: Names, helpers, invariants.
- Communication: Think-aloud; clarify constraints.
Pro tip Use 15–30m sprints. If stuck ~2 minutes, shrink the example or consider an alternative pattern.
Try a timed mock interview → Free
Suggested Practice Flow (7-Day Plan)
Day 1: Arrays + Sliding Window (Q1–Q12)
Day 2: Stack + Monotonic Stack (Q13–Q18)
Day 3: Binary Search + Hashing (Q19–Q27)
Day 4: Trees (Q28–Q33)
Day 5: Graphs (Q34–Q39)
Day 6: Heaps/Greedy (Q40–Q44)
Day 7: Dynamic Programming (Q45–Q50) + full mock
FAQ
Continue learning: What is MockQube · Pricing