Home · Blog · Technical and coding
Data structures to know for coding interviews
Most coding interview problems are solved with a small set of data structures. Knowing what each one is good at, and what it costs, is often the difference between seeing the solution and staring at the problem. Here's the list, with when to reach for each.

In short
- Arrays, strings and hash maps solve a large share of interview problems.
- Stacks, queues, heaps, trees and graphs cover most of the rest.
- For each one, know the main operations and their cost.
- When stuck, go through the list and ask which structure would make the slow step fast.
Practise this free: a mock interview for Coding round (algorithms), scored, with a stronger answer. 3 a month, no card.
Start a mock interviewYou don’t need every data structure from a textbook for coding interviews. A small set covers the vast majority of problems. What matters is knowing each one’s strengths and costs well enough to pick the right one quickly.
For each structure below: what it is, the costs to know and the problems where it shows up. Costs use big O notation; if that’s new, start with our guide to big O notation for interviews.
Arrays and strings
What: items stored in order, accessed by index.
Costs: read by index O(1); append at the end O(1) on average; insert or delete in the middle O(n); search an unsorted array O(n).
Shows up in: almost everything. Two pointers, sliding windows, prefix sums, in-place reversal, sorting first.
Watch out: building strings in a loop can be O(n squared) in some languages; use a list and join.
Hash maps and hash sets
What: key to value lookups (map) or membership checks (set).
Costs: insert, lookup and delete O(1) on average.
Shows up in: counting, finding duplicates, two-sum style problems, grouping anagrams, caching results.
Tip: when a solution has a nested loop that only searches for something, a hash map usually removes the inner loop. Wikipedia’s article on hash tables explains why lookups are fast on average.
Stacks
What: last in, first out.
Costs: push and pop O(1).
Shows up in: matching brackets, undo operations, evaluating expressions, “next greater element” problems with a monotonic stack, and turning recursion into iteration.
Queues and deques
What: first in, first out; a deque allows both ends.
Costs: add and remove at the ends O(1) (use a proper deque, not removing from the front of an array).
Shows up in: breadth-first search, processing in order, sliding window maximum.
Linked lists
What: nodes pointing to the next node.
Costs: insert or delete with a pointer to the node O(1); find by position O(n).
Shows up in: reversing a list, detecting cycles with fast and slow pointers, merging sorted lists. Mostly an interview topic in itself; you’ll implement them by hand.
Trees and binary search trees
What: nodes with children. In a binary search tree, left is smaller and right is larger.
Costs: balanced BST search, insert and delete O(log n); unbalanced can degrade to O(n).
Shows up in: traversals (in-order, pre-order, level-order), depth and diameter, lowest common ancestor, validating a BST. Many tree problems are recursion problems.
Heaps (priority queues)
What: gives you the smallest (or largest) item quickly.
Costs: peek O(1); insert and remove O(log n); build from a list O(n).
Shows up in: top k items, merging k sorted lists, scheduling, running median, Dijkstra’s shortest path.
Graphs
What: nodes and edges, stored as an adjacency list (usually) or matrix.
Costs: BFS and DFS are O(V + E), vertices plus edges.
Shows up in: grids (islands, flood fill), dependencies (topological sort), shortest paths, connected components.
Tip: many problems are graphs in disguise, such as a grid, a word ladder or course prerequisites.
Less common, worth knowing
- Tries: prefix searches and autocomplete.
- Union-find: grouping connected items, detecting cycles in undirected graphs.
- Sorted structures: when you need order and fast updates.
Know your language’s versions
In most interviews you’ll use the built-in structures, so know their names and costs in your language:
- Python: list (dynamic array), dict and set (hash map and set), collections.deque (queue), heapq (a min-heap on a list), and sorted() for sorting.
- Java: ArrayList, HashMap and HashSet, ArrayDeque for stacks and queues, PriorityQueue for heaps, TreeMap when you need sorted keys.
- JavaScript: arrays, Map and Set (prefer them over plain objects for lookups), and no built-in heap, so be ready to write a small one or explain how you would.
- C++: vector, unordered_map and unordered_set, deque, priority_queue (a max-heap by default), and map and set for sorted data.
Two details trip people up: removing from the front of a Python list is O(n), so use a deque, and Java’s PriorityQueue and C++'s priority_queue have opposite defaults (min versus max). Saying these things out loud shows you’ve used the tools, not only read about them.
Choosing the right one
When you’re stuck, ask what the slow step is, then which structure makes it fast:
“The slow part is checking whether I’ve seen a value before, which is O(n) in a list. A hash set makes that O(1), so the whole thing goes from O(n squared) to O(n). I’ll trade O(n) memory for it.”
That kind of reasoning is exactly what interviewers want to hear. For more techniques when the answer isn’t obvious, see what to do when stuck in a coding interview.
A quick self-test
Before an interview, check that you can answer these in a sentence each: When would you use a heap instead of sorting? Why is BFS the right choice for the shortest path in an unweighted graph? What happens to a binary search tree’s performance if you insert sorted data? How would you find duplicates in O(n)? If any answer takes more than a few seconds, revisit that section.
Practise
Learn the structures in the order of our LeetCode study plan, a few problems each. For a general reference, Wikipedia’s overview of data structures lists many more than you’ll need. Check what your target role emphasises in our software engineer and backend developer question guides.
Practise explaining your choices, free. Ghostpilot’s mock interviews ask technical questions for your role and score how clearly you explain why you picked an approach.
3 mock interviews a month are free, with no card needed.
Start freeQuestions
What are the most important data structures for coding interviews?
Arrays, strings and hash maps come first, then stacks and queues, linked lists, trees (especially binary search trees), heaps and graphs. Tries and union-find appear less often but are worth knowing for senior roles.
Do I need to implement data structures from scratch in interviews?
Sometimes: linked lists, binary trees and occasionally a heap or trie. More often you use the language's built-in versions, so know them well, along with their costs.
Which data structure should I use to make lookups faster?
Usually a hash map or hash set, which gives average constant-time lookups. If you also need order, a balanced binary search tree or a sorted array with binary search may fit better.
How long does it take to learn data structures for interviews?
If you program already, two to three weeks of focused study and practice covers the essentials. Learning to recognise which one fits a problem takes longer and comes from practice.
Get live help in your next interview
Ghostpilot hears each question and shows an answer from your CV, in a small window above your call. Free to start: 2 live interviews and 3 mock interviews a month, no card needed.
Keep reading



