Data structures: practice questions with explanations
Free Data structures multiple-choice practice questions with hints and explanations, at three levels: key terms and facts, applying the concepts, cases and analysis.
A data structure is a way to organise data in memory so that certain operations are fast: finding an item, adding one, removing one.
No structure is fast at everything. Choosing one means choosing which operations should be cheap. Their costs are written in Big O, with n the number of items.
The two basic building blocks are the array and the linked list.
An array stores its items in one block of memory, one after another. The computer can calculate where item i is, so reading a[i] takes O(1). Inserting at the front means shifting every item: O(n).
A linked list stores each item in a node with a pointer to the next node. The last pointer is null. Inserting at the head (h) only changes one pointer: O(1). But to reach item i you must follow i pointers: O(n).
A doubly linked list also has a pointer to the previous node, so you can walk both ways.
Self-check Which operation is O(1) for an array but O(n) for a singly linked list?
Counting the items one by one
Inserting a new item at the front
Reading the item at index i
Visiting every item once
Answer: Reading the item at index i. An array computes the address of item i directly. A linked list has to follow i pointers from the head. Inserting at the front is the opposite case: cheap for a list, expensive for an array.
2Stacks and queues
A stack is like a pile of plates: you add and remove only at the top. The last item in is the first item out: LIFO.
push puts an item on top, pop removes the top item, peek reads the top without removing it. All are O(1).
Uses: the undo function of an editor, the call stack of a program, checking that brackets are balanced, depth-first search.
A queue is like a line at a till: you join at the back and leave at the front. The first item in is the first item out: FIFO.
enqueue adds at the back, dequeue removes from the front. Both are O(1).
Uses: print jobs, messages waiting to be handled, breadth-first search.
A deque (double-ended queue) allows adding and removing at both ends. A priority queue always removes the item with the highest priority first; it is usually built as a heap.
Self-check Which data structure fits the “undo” button of a text editor best?
A graph
A hash table
A queue
A stack
Answer: A stack. Undo must reverse the most recent action first: last in, first out. A queue would undo the oldest action first.
3Trees, heaps, hash tables and graphs
A tree has one top node, the root. Every other node has exactly one parent. Nodes without children are leaves. A tree with n nodes has n − 1 edges.
In a binary tree every node has at most two children. A binary search tree (BST) adds a rule: keys in the left subtree are smaller than the node, keys in the right subtree are larger.
Searching a BST follows one path down, so it costs O(height). A balanced tree (AVL, red-black) keeps the height near log₂ n. Keys inserted in sorted order make a chain of height n.
A heap is a binary tree where every parent is smaller than its children (min-heap) or larger (max-heap). The minimum or maximum is at the root, and insert and remove cost O(log n).
A hash table stores key–value pairs. A hash function turns the key into an index, so insert and lookup take O(1) on average. Two keys with the same index are a collision, solved by chaining (a list per slot) or open addressing (try another slot).
A graph has vertices joined by edges, which may have a direction or a weight. It is stored as an adjacency matrix, O(V²) memory, or as adjacency lists, O(V + E).
Self-check The keys 50, 30, 70, 20 and 40 are inserted in this order into an empty binary search tree. What is the left child of 30?
20
40
70
It has no left child
Answer: 20. 20 is smaller than 50, so it goes left to 30. It is also smaller than 30, so it goes left again. 40 is larger than 30 and becomes its right child.
4The method step by step
Questions often describe a task and ask which structure fits, or ask what a structure looks like after some operations.
Find the operation that must be fast: access by position, by key, the newest item, the oldest item, the smallest item, or a range.
Match it to a structure: index → array, key → hash table, newest → stack, oldest → queue, smallest or largest → heap, sorted order or ranges → balanced search tree.
Check the cost of the other operations the task needs, in Big O.
For a trace question, carry out the operations one at a time and write down the state after each one.
Check the final state against the rule of the structure: LIFO, FIFO, the BST order or the heap order.
5A worked example
Follow every step. Try to predict the next one before you open it.
Worked example
The keys 50, 30, 70, 20, 40 and 60 are inserted in this order into an empty binary search tree. What is the in-order traversal, and how many comparisons does a search for 60 need?
1
Insert the root
The first key, 50, becomes the root.
2
Insert 30 and 70
30 is smaller than 50, so it goes to the left. 70 is larger than 50, so it goes to the right.
3
Insert 20, 40 and 60
20: smaller than 50, smaller than 30, so left of 30. 40: smaller than 50, larger than 30, so right of 30. 60: larger than 50, smaller than 70, so left of 70.
4
Traverse in order
An in-order traversal visits the left subtree, then the node, then the right subtree.
That gives 20, 30, 40, 50, 60, 70: the keys in sorted order, as it always is for a BST.
5
Search for 60
Compare with 50: larger, go right. Compare with 70: smaller, go left. Compare with 60: found.
The search needs 3 comparisons, one per level.
Answer
In-order: 20, 30, 40, 50, 60, 70. The search for 60 needs 3 comparisons.
6Common mistakes
Mixing up LIFO and FIFO. A stack returns the newest item, a queue the oldest.
Thinking a linked list gives fast access by index. It must walk from the head.
Assuming a BST is always O(log n). Only a balanced tree is; sorted inserts give O(n).
Forgetting collisions in a hash table. O(1) is the average, not the worst case.
Mixing up the traversals. Preorder: node first. In-order: node in the middle. Postorder: node last.
Using an adjacency matrix for a large sparse graph. It wastes O(V²) memory.
Self-check You enqueue 1, 2 and 3 into an empty queue, in this order. Which item does the first dequeue return?
1, 2 and 3 together
1
2
3
Answer: 1. A queue is first in, first out, so 1 leaves first. 3 would be the answer for a stack (pop), which returns the newest item.
7How exam questions look
Easy: key terms and facts. LIFO and FIFO, what a node, root or leaf is, the BST rule, what a hash function does.
Medium: applying the concepts. Which structure fits a task, the result of push and pop or of BST inserts, tree traversals, a hash table with linear probing, costs in Big O.
Hard: cases and analysis. Balanced trees, heaps in arrays, B-trees, tries, union-find, Bloom filters and designs such as an LRU cache.
For a heap stored in an array from index 0, the children of index i are at 2i + 1 and 2i + 2, and its parent is at ⌊(i − 1) / 2⌋.
What is the main difference between a static array and a dynamic array?
AA static array allows slower index access
BA dynamic array can grow by moving to a larger block of memory
CA dynamic array stores its elements in linked nodes so it can grow one at a time
DA static array can only store numbers
Show hints
Python lists and Java ArrayLists are examples of one of them.
Two wrong answers are now crossed out. One of the remaining two is right.
Show the answer and explanation
Why this is the answer
A static array has a fixed size; a dynamic array allocates a bigger array and copies the elements when it is full.
Answer
B · A dynamic array can grow by moving to a larger block of memory
Example 2Medium
How does extract-min work in a binary min-heap?
ARemove the last element of the array
BMove the last element to the root and sift it down
CRemove the root and shift all elements one place left
DSearch all leaves for the new minimum
Show hints
Keep the tree complete, then fix the order.
Two wrong answers are now crossed out. One of the remaining two is right.
Show the answer and explanation
Why this is the answer
The last element fills the hole at the root and is swapped with its smaller child until the heap property holds, taking O(log n).
Answer
B · Move the last element to the root and sift it down
Example 3Hard
Without union by rank or path compression, what is the worst-case cost of a single find in a union-find structure with n elements?
AO(1)
BO(α(n))
CO(log n)
DO(n)
Show hints
The trees can degenerate.
Two wrong answers are now crossed out. One of the remaining two is right.
Show the answer and explanation
Why this is the answer
Naive unions can link trees into a long chain, so find may follow n − 1 parent pointers; the two heuristics bring it close to constant.
Answer
D · O(n)
Questions and answers
What is Data structures?
A data structure is a way to organise data in memory so that certain operations are fast: finding an item, adding one, removing one.
No structure is fast at everything. Choosing one means choosing which operations should be cheap. Their costs are written in Big O, with n the number of items.
How do I learn Data structures step by step?
What a data structure is
Stacks and queues
Trees, heaps, hash tables and graphs
The method step by step
A worked example
Common mistakes
How exam questions look
What do the Data structures exercises cover?
Easy: key terms and facts
Medium: applying the concepts
Hard: cases and analysis
Every question has four options, and only one is right. Stuck? Open a small hint first, and the full explanation only when you need it.
How practising works
Every question has four options, and only one is right.
Stuck? Open a small hint first, and the full explanation only when you need it.
Your progress, mistakes and saved problems are kept, so you can review before the exam.