Hack4Hack

DSA interview questions: the patterns, the classics and how to answer them

By Vikram · Updated 26 Oct 2026

Coding interviews reuse a small set of problem patterns. Once you can spot the pattern, a new problem becomes a variation of one you have already solved. Here are the patterns that matter most, the classic question for each and the complexity interviewers expect.

Complexity cheat sheet

OperationTime
Array access by indexO(1)
Hash map insert / lookup (average)O(1)
Binary search on a sorted arrayO(log n)
Heap push / popO(log n)
Building a heap from n itemsO(n)
Comparison sorting (merge sort, heap sort)O(n log n)
BFS / DFS on a graphO(V + E)
Dijkstra with a binary heapO((V + E) log V)

The patterns, with a classic question each

  • Hashing: Two Sum. Store each number's index in a map and look up target − x: O(n).
  • Two pointers: pair with a target sum in a sorted array, or reverse an array in place.
  • Sliding window: longest substring without repeating characters: grow the window, shrink it on a repeat.
  • Prefix sums: number of subarrays with sum k, using a map of prefix sums.
  • Stack: valid parentheses; next greater element with a monotonic stack.
  • Binary search: search in a rotated sorted array, or binary search on the answer (e.g. minimum capacity).
  • Linked lists: detect a cycle with fast and slow pointers; reverse a list; merge two sorted lists.
  • Trees: height, level-order traversal with a queue, lowest common ancestor, validate a BST (inorder is sorted).
  • Graphs: number of islands (DFS/BFS on a grid), course schedule (topological sort), shortest path (BFS unweighted, Dijkstra weighted).
  • Heaps: k largest elements with a min-heap of size k: O(n log k).
  • Dynamic programming: climbing stairs, coin change, longest common subsequence, 0/1 knapsack.
  • Backtracking: subsets, permutations, N-Queens.

How to answer a DSA question in an interview

  1. Repeat the problem and ask about the input: size, duplicates, negatives, empty input.
  2. Say the brute-force solution and its complexity first.
  3. Find the bottleneck and name the pattern that removes it.
  4. Code it cleanly, then walk through a small example by hand.
  5. State the final time and space complexity, and the edge cases you handled.

Tip: Test yourself on complexity and output questions every day: the DSA questions with answers page collects every DSA question from the Daily Quiz, each with an explanation.

Frequently asked questions

What are the most important DSA topics for interviews?

Arrays and strings, hashing, two pointers, sliding window, stacks, binary search, linked lists, trees, graphs (BFS and DFS), heaps and dynamic programming.

What is the time complexity of binary search?

O(log n): each step halves the remaining search range.

Which data structure does BFS use?

A queue (first in, first out). DFS uses a stack or recursion.

How do I get better at dynamic programming?

Start with 1-D problems (climbing stairs, house robber), define the state and the recurrence in words before coding, then move to 2-D problems like LCS and knapsack.

Play the Daily Quiz