Choosing the right structure
Choosing a data structure in an interview is not a question of "which one do I know best". You ask one question and the answer falls out:
"Which operation repeats most in this problem?"
The reason is simple: every structure makes some operations O(1) and others O(n). The most repeated operation must be the cheap one; the rest can be whatever they are. If you do 1000 key lookups and one sort, take the hash map — the sort being O(n log n) changes nothing.
A three-step framework:
- 1. List the operations — what does the problem actually do: insert, delete, find by key, take the minimum, ask a range, iterate in order?
- 2. Estimate the frequency — which of them sits inside the loop, i.e. repeats
ntimes? - 3. Pick the structure that makes that operation cheap — and say out loud which operations you are making expensive in exchange
That last half of step three is the most commonly skipped: every choice is a trade-off, and naming your own trade-off is a strong signal.
| Signal in the problem | Structure | Why |
|---|---|---|
| "Find by key", "does this value exist?", "how often does it occur?" | Hash map / hash set | `O(1)` average lookup and insert; no ordering kept |
| "Remove duplicates", "count the unique ones" | Set | `O(1)` membership, no need to store values |
| "The most recently added", "undo", "are the brackets balanced" | Stack (LIFO) | Only the last element matters — `O(1)` push/pop |
| "Process in arrival order", "traverse layer by layer" | Queue (FIFO) | The basis of BFS and every in-order processing problem |
| "Top K", "repeatedly take the minimum", "streaming median" | Heap (priority queue) | `O(log n)` insert, `O(1)` peek at the minimum; no full sort needed |
| "Values between X and Y", "nearest value", "iterate in order" | Sorted array / balanced tree (TreeMap, SplayTreeSet) | Order is preserved — range queries in `O(log n)`; a hash map cannot do this |
| "Starts with this prefix", "autocomplete" | Trie (prefix tree) | Lookup depends on the prefix length, not on the number of words |
| "Frequent insert/delete in the middle", "no indexing needed" | Linked list (or deque) | `O(1)` removal when you hold the node; but indexed access is `O(n)` |
| "Are these two in the same group", "merge the groups" | Union-Find (disjoint set) | Union and find are effectively `O(1)` |
A table makes the choice easy, but in an interview naming the trade-off is worth more. Every structure has a weak side and you should be the one to state it:
- Hash map —
O(1)lookup, but no ordering and extra memory. Answering "what is the smallest key?" requires scanning everything:O(n) - Heap — the minimum is cheap, but it is not a sorted list. Iterating a heap does not give you sorted order, and "the 3rd smallest" isn't a direct fit
- Sorted array — excellent for range queries and binary search, but inserting in the middle is `O(n)` because elements must shift
- Balanced tree — gives both order and
O(log n)inserts, but is slower than a hash map and adds complexity to the code - Trie — unbeatable for prefix search, but memory hungry; on a small dictionary a simple filter over a list is more practical
- Linked list —
O(1)removal from the middle, but cache-unfriendly; in practice small arrays beat linked lists
Keep these sentences ready: "I'll take a hash map because lookup dominates — I lose ordering in exchange, but this problem doesn't need ordering." When you name the trade-off yourself, the interviewer cannot hand it back to you as a caught mistake.
How to say this in an interview. Choosing a structure should be a 20-30 second paragraph — neither silence nor a five-minute lecture.
A four-sentence template:
- (1) Name the dominant operation: "Inside the loop I ask, for each element, 'have I seen this value before' — so the lookup repeats
ntimes." - (2) Pick the structure and state the complexity: "So I'll take a hash set:
O(1)average lookup,O(n)overall." - (3) Name the trade-off: "In exchange I spend
O(n)extra memory and lose the ordering of the elements." - (4) Reject the alternative — with a reason: "I could sort and use two pointers instead,
O(n log n)time andO(1)memory; if the input were huge and memory were the constraint, I'd take that one."
The fourth sentence is what lifts the answer above average: you don't just know one solution, you choose between two according to context.
The most common mistake: picking a structure without stating why. When the interviewer asks "why a hash map?", "because it's fast" is weak — "because key lookup is the dominant operation here and a hash map makes it O(1)" is strong. The difference is one sentence, but it is large in the scoring.
Interview tip: while you talk through the brute force, ask yourself one question — "which operation here is O(n), and is there a structure that makes it O(1)?" The vast majority of optimisations come out of that single question: a nested loop (O(n²)) usually drops to O(n) with a hash map, repeatedly searching for a minimum drops to O(log n) with a heap, and repeatedly asking "what is in this range" drops to O(log n) with an ordered structure. Optimisation isn't magic — it's finding the expensive operation and picking the structure that fits it.