Top Data Structures Interview Questions
Short answer: interviewers expect you to know when to use each core data structure and its time complexity. Be ready to explain arrays, linked lists, stacks, queues, hash tables, trees, heaps and graphs, and to solve problems with them.
Complexity cheat table
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) |
| Linked list | O(n) | O(n) | O(1) at known node/head | O(1) at known node |
| Stack / queue | O(n) | O(n) | O(1) | O(1) |
| Hash table | n/a | O(1) average | O(1) average | O(1) average |
| Balanced BST | n/a | O(log n) | O(log n) | O(log n) |
| Binary heap | O(1) min/max | O(n) | O(log n) | O(log n) |
1. Array vs linked list
Arrays store elements contiguously, giving fast index access but slow insertion in the middle. Linked lists store nodes with pointers, so insertion and deletion are cheap once you hold the node, but access requires walking the list and uses more memory per element.
2. How do you reverse a linked list?
function reverse(head) {
let prev = null;
while (head) {
const next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
Time O(n), space O(1). Related questions: detect a cycle (fast and slow pointers) and find the middle node.
3. Stack vs queue
- Stack: last in, first out (LIFO). Used for undo, recursion, balanced parentheses.
- Queue: first in, first out (FIFO). Used for task scheduling and breadth-first search.
function isBalanced(s) {
const pairs = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const ch of s) {
if ('([{'.includes(ch)) stack.push(ch);
else if (stack.pop() !== pairs[ch]) return false;
}
return stack.length === 0;
}
4. How does a hash table work?
A hash function turns a key into an array index. Collisions (two keys mapping to the same index) are handled by chaining (a list per bucket) or open addressing. Average operations are O(1), worst case O(n). It powers dictionaries, sets and caches.
// Two-sum with a hash map: O(n)
function twoSum(nums, target) {
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const need = target - nums[i];
if (seen.has(need)) return [seen.get(need), i];
seen.set(nums[i], i);
}
}
5. Binary tree vs binary search tree
A binary tree gives each node at most two children. A binary search tree (BST) keeps left values smaller and right values larger, enabling O(log n) search when balanced (AVL and red-black trees stay balanced). In-order traversal of a BST returns sorted values.
6. Tree traversals
| Traversal | Order |
|---|---|
| In-order | Left, node, right |
| Pre-order | Node, left, right |
| Post-order | Left, right, node |
| Level-order (BFS) | Level by level, using a queue |
7. What is a heap?
A complete binary tree where each parent is smaller (min-heap) or larger (max-heap) than its children. It implements a priority queue and solves "top K elements" and "merge sorted lists" problems efficiently.
8. Graph basics
- Represent graphs as adjacency lists (space-efficient) or adjacency matrices (fast edge lookup).
- BFS finds shortest paths in unweighted graphs; DFS explores depth first and detects cycles.
- Dijkstra finds shortest paths with non-negative weights; topological sort orders dependencies.
9. What is a trie?
A tree where each node represents a character, used for fast prefix search and autocomplete.
10. How do you choose a data structure?
| Need | Use |
|---|---|
| Fast lookup by key | Hash table |
| Sorted data with fast insert/search | Balanced BST |
| Always get the smallest or largest | Heap |
| Process in arrival order | Queue |
| Undo / backtracking | Stack |
| Relationships and networks | Graph |
How to answer in an interview
- Clarify the problem and constraints.
- State a simple (brute force) approach, then improve it.
- Explain time and space complexity.
- Write clean code and test with edge cases (empty input, duplicates, one element).
Next steps
Find practice sites in 10 sites for interview preparation and plan your study with how to prepare for a technical interview.
