Loading…
Loading…
Charting your lesson…
Opening your lesson…
Choosing the Right Data Structure
Lesson 3 of 4
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Most coding-interview optimisations come down to one move: picking the data structure whose strengths match the operation you keep repeating. Candidates who know the cost of each operation can look at a brute force, spot the expensive step, and swap in the right structure almost mechanically. This is the highest-leverage knowledge in a coding round.
What this covers / why it matters: the core data structures, their operation costs, and how to match a structure to a problem's needs. It turns "how do I speed this up?" into a lookup.
Step 2/ 61 min
| Structure | Lookup | Insert | Delete | Ordered? |
|---|---|---|---|---|
| Array | O(1) by index, O(n) by value | O(n) | O(n) | yes |
| Hash map / set | O(1) avg | O(1) | O(1) | no |
| Balanced BST / TreeMap | O(log n) | O(log n) | O(log n) | yes |
| Heap (priority queue) | O(1) peek min/max | O(log n) | O(log n) | partial |
| Stack | O(1) top | O(1) | O(1) | LIFO |
| Queue / Deque | O(1) ends | O(1) | O(1) | FIFO |
Read the problem for its dominant operation:
Hard problems often need two structures working together. An LRU cache is a hash map (O(1) lookup) plus a doubly linked list (O(1) reordering). A median-of-a-stream uses two heaps. Recognising when to pair structures — one for fast lookup, one for ordering — is the mark of a strong candidate.
Step 3/ 61 min
"Return the k most frequent elements in an array."
Reasoning by operations: first I need counts — "how many times have I seen x?" → a hash map (O(n) to build). Then I need the top k by count — "largest so far" → a heap. Push each (count, value) into a min-heap of size k; if it exceeds k, pop the smallest. That's O(n log k).
"I chose a hash map because counting is a pure lookup-and-increment (O(1) each), and a size-k min-heap because I only ever need the k largest, so I don't pay to fully sort all n distinct values." Two structures, each matched to one operation.
Step 4/ 61 min
Step 5/ 61 min
Choosing a data structure by familiarity rather than by the operation you're repeating — e.g. doing an O(n) linear search inside a loop when a hash set gives O(1). Always name the hot operation first, then pick the structure whose cost for that operation is lowest.
Step 6/ 6· Worked examples1 min
Problem/Question: "Design the data structure behind a browser autocomplete that suggests words as the user types a prefix." — Which structure, and why?
Solution/Model answer:
Reasoning through options:
Recall
Keep this
Half of algorithm interviews are really "pick the right data structure." The optimal solution usually falls out once you choose a structure whose strengths match the operations the problem needs most (fast lookup? ordering? min/max? prefix?).
| Need | Reach for |
|---|---|
| O(1) lookup / dedup / seen-before | Hash set / hash map |
| Ordered data, range queries | Sorted array / balanced BST / TreeMap |
| Repeated min or max | Heap (priority queue) |
| LIFO / undo / matching brackets | Stack |
| FIFO / BFS / sliding window | Queue / deque |
| Prefix relationships, autocomplete | Trie |
| Connectivity / grouping | Union-Find (DSU) |
Ask: which operation runs most often? Optimise the structure for that.
Expected pattern: Implicit in most problems; sometimes explicit ("what data structure would you use and why?").
Quick recap: Identify the most-frequent operation, then pick the structure that makes it cheap; state the space/time trade-off.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.