Top 50 Most Common DSA Interview Questions & Patterns

Last updated: · ~12 min read

Start a free mock interview

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.

Table of Contents

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

  1. Two Sum (hash) — O(n), O(n)
  2. Best Time to Buy & Sell Stock (I)O(n), O(1)
  3. Maximum Subarray (Kadane)O(n), O(1)
  4. Product of Array Except SelfO(n), O(1)
  5. Contains Duplicate (set) — O(n), O(n)
  6. Valid AnagramO(n), O(Σ)
  7. Longest Substring Without RepeatingO(n), O(Σ)
  8. Minimum Window SubstringO(n), O(Σ)
  9. 3Sum (sort+two pointers) — O(n²), O(1)
  10. Container With Most WaterO(n), O(1)
  11. Dutch National FlagO(n), O(1)
  12. Merge IntervalsO(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

  1. Valid Parentheses — O(n), O(n)
  2. Min Stack — O(1) ops
  3. Daily Temperatures — O(n), O(n)
  4. Next Greater Element — O(n), O(n)
  5. Largest Rectangle in Histogram — O(n), O(n)
  6. 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;
}

  1. Binary Search — O(log n)
  2. Search in Rotated Sorted Array — O(log n)
  3. Find Kth Smallest/Largest (Quickselect) — avg O(n)
  4. Answer-space BS (Koko/Ship Capacity) — O(n log A)
  5. 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

  1. Subarray Sum Equals K — O(n), O(n)
  2. Longest Consecutive Sequence — O(n), O(n)
  3. Group Anagrams — O(n·k log k) or O(n·k)
  4. Two Sum II (sorted) — O(n), O(1)

5) Trees (BST/BT) — DFS/BFS

  1. Level Order (BFS) — O(n), O(n)
  2. Max Depth (DFS) — O(n)
  3. Validate BST (bounds) — O(n)
  4. LCA (BST/BT) — O(h)/O(n)
  5. Diameter — O(n)
  6. 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

  1. Number of Islands — O(mn)
  2. Clone Graph — O(V+E)
  3. Course Schedule (Kahn) — O(V+E)
  4. Rotting Oranges — O(mn)
  5. Pacific Atlantic — O(mn)
  6. 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

  1. Kth Largest — heap O(n log k)
  2. Top K Frequent — heap/bucket O(n log k)/O(n)
  3. Task Scheduler — O(n)
  4. Meeting Rooms II — O(n log n)
  5. 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

  1. Climbing Stairs — O(n), O(1)
  2. House Robber I/II — O(n), O(1)
  3. Coin Change — O(amount·n)
  4. Longest Increasing Subsequence — O(n log n)
  5. Edit Distance — O(mn)
  6. 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

Want feedback like a real interviewer?
MockQube gives rubric-based coaching on solution approach, code quality, and communication—mirroring real loops.

Start your first mock now


FAQ

Continue learning: What is MockQube · Pricing