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.

Free practice problemEasy

What is the max-heap property?

Next problem →
Practise Data structures for freePractise Data structures now5 free problems every day. No card needed.

What you practise

  1. Easykey terms and facts
  2. Mediumapplying the concepts
  3. Hardcases and analysis

Learn Data structures from scratch

Before this: Algorithms & complexity

1What a data structure is

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.

a7[0]3[1]9[2]4[3]7394∅h

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?
  1. Counting the items one by one
  2. Inserting a new item at the front
  3. Reading the item at index i
  4. 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?
  1. A graph
  2. A hash table
  3. A queue
  4. 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?
  1. 20
  2. 40
  3. 70
  4. 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.

  1. Find the operation that must be fast: access by position, by key, the newest item, the oldest item, the smallest item, or a range.
  2. 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.
  3. Check the cost of the other operations the task needs, in Big O.
  4. For a trace question, carry out the operations one at a time and write down the state after each one.
  5. 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.

503070
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.

503070204060
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.

503070204060

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. 1, 2 and 3 together
  2. 1
  3. 2
  4. 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⌋.

Learn it step by step in the appLearn it step by step in the app

Example questions

Example 1Easy

What is the main difference between a static array and a dynamic array?

  1. AA static array allows slower index access
  2. BA dynamic array can grow by moving to a larger block of memory
  3. CA dynamic array stores its elements in linked nodes so it can grow one at a time
  4. DA static array can only store numbers
Show hints
  1. Python lists and Java ArrayLists are examples of one of them.
  2. 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?

  1. ARemove the last element of the array
  2. BMove the last element to the root and sift it down
  3. CRemove the root and shift all elements one place left
  4. DSearch all leaves for the new minimum
Show hints
  1. Keep the tree complete, then fix the order.
  2. 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?

  1. AO(1)
  2. BO(α(n))
  3. CO(log n)
  4. DO(n)
Show hints
  1. The trees can degenerate.
  2. 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?
  1. What a data structure is
  2. Stacks and queues
  3. Trees, heaps, hash tables and graphs
  4. The method step by step
  5. A worked example
  6. Common mistakes
  7. 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.

Topics before and after this one

Learn first

Courses with this topic

More topics in Computer science

Printable worksheet

Practise Data structures for freePractise Data structures now5 free problems every day. No card needed.