Separate the container from the movement.
A data structure describes how information is organized and what operations are cheap. An algorithm describes the steps used to produce an answer. An interview pattern is a reusable shape for those steps. A graph can be stored as an adjacency list; DFS and BFS are two different ways to traverse it.
What does this structure make cheap, and what does it make expensive? That answer often explains why one algorithm fits better than another.
1. Build the data-structure foundations
Arrays & strings
Direct indexing is usually O(1). Full scans are O(n). Inserting or removing in the middle may require shifting values.
Hash maps & sets
Use keys for fast expected lookup, frequency counting, membership, grouping, and remembering what you have seen.
Linked lists, stacks & queues
Follow nodes, remember unfinished work with a stack, or preserve arrival order with a queue.
Trees
Practice recursive structure, subtree questions, traversal orders, and the difference between height and number of nodes.
Graphs
Represent vertices and edges, prevent repeated visits, find connected components, and reason about paths.
Heaps
Keep the next minimum or maximum accessible while values enter and leave, often in O(log n) per update.
Complexity changes with the representation
For a graph, an adjacency list lets a traversal inspect the neighbors that actually exist, leading to O(V + E) work. An adjacency matrix may require scanning a full row of possible neighbors for every vertex, which can move toward O(V²). The abstract graph is the same; the representation changes the work.
2. Learn the movement patterns
Once the structures are familiar, focus on how state moves through them.
| Pattern | Main state | Progress |
|---|---|---|
| Two pointers | Two useful positions | Move the side that removes impossible work |
| Sliding window | Range boundaries plus window state | Add what enters, remove what leaves |
| Binary search | Left, right, and middle | Discard one impossible half |
| DFS | Call stack or explicit stack | Finish one branch before returning |
| BFS | Queue and visited set | Finish one distance layer before the next |
| Backtracking | Current candidate and choices | Choose, explore, undo, prune |
| Dynamic programming | State definition and stored answers | Build from smaller overlapping subproblems |
The same pattern can appear on different surfaces. DFS can explore a tree, a graph, a grid, or an implicit state space. Sliding window can track a sum, a frequency map, a count of invalid items, or another compact summary of a contiguous range.
3. Practice the strategy layer
Restate the input, output, constraints, and edge cases before committing to an approach.
Name a simple solution first, then identify the repeated or unnecessary work an optimized pattern removes.
Describe initialization, the main loop or recursion, state updates, termination, and return value.
Count the dominant work and the helper storage that grows with the input.
Big-O is part of the explanation, not a label added at the end. If you say O(n), you should be able to explain what enters the scan once. If you say O(log n), you should be able to identify what is halved each round.
Read the practical Big-O guide →
A sensible DSA study order
- Arrays, strings, hash maps, and sets: build comfort with indexing, scans, counts, and lookup.
- Two pointers, sliding window, and binary search: practice loop invariants and boundary movement.
- Linked lists, stacks, queues, trees, and recursion: learn references, call stacks, and hierarchical state.
- Graphs, DFS, and BFS: add visited state, components, layers, and representation choices.
- Heaps, intervals, greedy, and backtracking: compare priority, ordering, local choices, and search trees.
- Dynamic programming: define state and transitions after recursion and overlapping subproblems feel concrete.
- Mixed review: practice recognition without being told the topic and revisit mistakes with spacing.
After each new concept, solve a small set of focused problems and explain the invariant in your own words. Breadth without retrieval practice fades quickly.