Data structures worksheet with answers

Free printable Data structures worksheet: 8 problems from easy to exam level, with an answer key. Print it or save it as a PDF.

For teachers: free to print, copy and use in class. Every new worksheet has new values and its own answer key. A link to ExamProblems on your course page is appreciated.

Name: ____________________   Date: ____________

1.Easy

What does each node in a doubly linked list contain?

  1. AData, a pointer to the next node and a pointer to the previous node
  2. BData and two pointers, one to the next node and one to the node after that
  3. CData and a pointer to the next node only
  4. DTwo copies of the data and one pointer
2.Easy

In which order does a preorder traversal of a binary tree visit nodes?

  1. ALeft subtree, then right subtree, then node
  2. BLeft subtree, then node, then right subtree
  3. CLevel by level from the top
  4. DNode, then left subtree, then right subtree
3.Easy

Which data structure works on the principle "first in, first out"?

  1. ABinary tree
  2. BStack
  3. CQueue
  4. DHeap
4.Medium

What is the cost of inserting an element in the middle of an array of n elements?

  1. AO(log n), because of binary search
  2. BO(n), because later elements must shift
  3. CO(1), because the index is known
  4. DO(n²), because each element moves n times
5.Medium

What balance condition does an AVL tree maintain at every node?

  1. AThe heights of the left and right subtrees differ by at most 1
  2. BEvery node is coloured red or black
  3. CBoth subtrees of every node contain exactly the same number of nodes
  4. DAll leaves are at exactly the same depth
6.Medium

An application needs fast lookups and also needs to list keys in sorted order and answer range queries. Which structure fits best?

  1. AA balanced binary search tree
  2. BA hash table
  3. CA stack
  4. DAn unsorted linked list
7.Hard

In a circular buffer, front == rear can mean either empty or full. What is a common way to tell them apart?

  1. AKeep a separate element count, or always leave one slot unused
  2. BAlways clear the buffer whenever front equals rear, so the state is empty
  3. CMake the buffer size a power of two
  4. DStore the front and rear in the same variable
8.Hard

Inserting a key into an AVL tree makes a node's left subtree too tall, and the new key went into the left subtree of its left child (left-left case). Which fix is applied?

  1. AA single left rotation at the unbalanced node
  2. BRecolouring the node and its child
  3. CA left rotation at the child, then a right rotation at the node
  4. DA single right rotation at the unbalanced node

Answer key

  1. 1. A · Data, a pointer to the next node and a pointer to the previous node
  2. 2. D · Node, then left subtree, then right subtree
  3. 3. C · Queue
  4. 4. B · O(n), because later elements must shift
  5. 5. A · The heights of the left and right subtrees differ by at most 1
  6. 6. A · A balanced binary search tree
  7. 7. A · Keep a separate element count, or always leave one slot unused
  8. 8. D · A single right rotation at the unbalanced node

Full step-by-step solutions: practise this topic on examproblems.com.