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.

Ghostpilot team · · 4 min read

A dark blue card with the title "Data structures to know for coding interviews" next to the Ghostpilot ghost

In short

Practise this free: a mock interview for Coding round (algorithms), scored, with a stronger answer. 3 a month, no card.

Start a mock interview

You 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

Know your language’s versions

In most interviews you’ll use the built-in structures, so know their names and costs in your language:

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 free

Questions

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

Big O notation for interviews, explained simplyTechnical and coding · 4 min read
LeetCode study plan for interviews (8 weeks)Technical and coding · 4 min read
What to do when you're stuck in a coding interviewTechnical and coding · 4 min read
Technical interviews for non-technical rolesTechnical and coding · 4 min read