DSA · All levels
DSA Interview Preparation: Complexity, Core Structures and Problem Patterns
A structured way to prepare data structures and algorithms for coding rounds — Big-O, the essential data structures, and the problem-solving patterns that cover most interview questions.
Published 4 Dec 20263 min read
Coding rounds are less about memorising solutions and more about recognising patterns. Learn the building blocks, then practise identifying which pattern a new problem needs.
Big-O in one table
| Complexity | Name | Example |
|---|---|---|
| O(1) | Constant | Array index, hash map lookup (average) |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Single pass over an array |
| O(n log n) | Linearithmic | Efficient sorting (merge sort) |
| O(n²) | Quadratic | Nested loops over the same array |
| O(2ⁿ) | Exponential | Naive recursive subsets |
Always state time and space complexity, and mention the worst case.
Essential data structures
| Structure | Strength | Typical operations |
|---|---|---|
| Array / dynamic array | Fast index access | Access O(1), insert in middle O(n) |
| Hash map / set | Fast lookups | Insert/find O(1) average |
| Stack | Last-in, first-out | Matching brackets, undo, DFS |
| Queue / deque | First-in, first-out | BFS, sliding windows |
| Linked list | Cheap insert/delete at known nodes | Reversal, cycle detection |
| Heap (priority queue) | Fast min/max | Top-K, scheduling — O(log n) push/pop |
| Binary search tree | Ordered data | O(log n) when balanced |
| Graph | Relationships | BFS, DFS, shortest paths |
| Trie | Prefix search | Autocomplete |
The patterns that matter most
1. Two pointers — for sorted arrays or pairs: pair sum, removing duplicates, container with most water.
2. Sliding window — for contiguous subarrays/substrings: longest substring without repeating characters, max sum of size-k window.
def longest_unique(s):
seen, left, best = {}, 0, 0
for right, ch in enumerate(s):
if ch in seen and seen[ch] >= left:
left = seen[ch] + 1
seen[ch] = right
best = max(best, right - left + 1)
return best
3. Hashing — trade space for time: two-sum, anagram grouping, frequency counts.
4. Prefix sums — answer range-sum queries quickly; count subarrays with a given sum.
5. Binary search — on sorted data and on the answer space ("minimum capacity to ship within D days").
6. Fast & slow pointers — detect a cycle in a linked list, find the middle node.
7. Stack / monotonic stack — valid parentheses, next greater element, daily temperatures.
8. Trees: DFS & BFS — height, diameter, level-order traversal, lowest common ancestor.
9. Graphs — BFS for shortest path in unweighted graphs, DFS for connected components, topological sort for dependencies, Dijkstra for weighted shortest paths.
10. Heaps — top-K elements, merge K sorted lists, running median.
11. Backtracking — subsets, permutations, N-Queens: build a choice, recurse, undo.
12. Dynamic programming — overlapping subproblems + optimal substructure: climbing stairs, coin change, longest common subsequence, 0/1 knapsack. Start with recursion + memoisation, then convert to a table.
A repeatable approach in the interview
- Clarify — inputs, constraints, edge cases (empty input, duplicates, negatives).
- Examples — work one small example by hand.
- Brute force — state it and its complexity.
- Optimise — which pattern removes the repeated work?
- Code — clean names, small helper functions.
- Test — walk through your example and an edge case.
- Analyse — final time and space complexity.
Talk while you think — interviewers assess your reasoning, not just the final code.
A practical study plan
- Weeks 1–2: arrays, strings, hashing, two pointers, sliding window.
- Weeks 3–4: linked lists, stacks, queues, binary search.
- Weeks 5–6: trees, heaps, graphs.
- Weeks 7–8: backtracking and dynamic programming; timed mock interviews.
Solve fewer problems well: after each one, write down the pattern and the key insight, then revisit it a week later.
- #dsa
- #coding-interview
- #algorithms