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

The depth of a node in a tree is:

  1. AThe number of edges from that node to the deepest leaf
  2. BThe number of edges from the root to that node
  3. CThe number of nodes in its subtree
  4. DThe number of children of that node
2.Easy

What is the defining feature of a set as an abstract data type?

  1. AIt stores elements in sorted order
  2. BIt stores elements without duplicates
  3. CIt stores key-value pairs
  4. DIt allows access only at one end
3.Easy

In a singly linked list, what does the next pointer of the last node contain?

  1. AThe address of the previous node
  2. BThe number of nodes in the list
  3. CThe address of the first node
  4. DNull (no node)
4.Medium

A binary tree has root 1 with left child 2 and right child 3; node 2 has children 4 (left) and 5 (right). What is the preorder traversal?

  1. A4 2 5 1 3
  2. B1 2 4 5 3
  3. C1 2 3 4 5
  4. D4 5 2 3 1
5.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 hash table
  2. BA balanced binary search tree
  3. CA stack
  4. DAn unsorted linked list
6.Medium

How many nodes does a perfect binary tree of height h have, with height counted in edges?

  1. A2h
  2. B2h + 1
  3. C2h+1 − 1
  4. Dh2
7.Hard

In the BST with root 50, left subtree 30 (children 20, 40) and right subtree 70 (children 60, 80), the root is deleted using the in-order successor. Which key becomes the new root?

  1. A60
  2. B80
  3. C70
  4. D40
8.Hard

You must keep the k smallest values from a stream of n numbers using O(k) memory. Which approach works in O(n log k) time?

  1. AA FIFO queue of the last k values
  2. BA min-heap holding all n values, from which the k smallest are popped at the end
  3. CA max-heap of size k, replacing its root when a smaller value arrives
  4. DA sorted array of all n values

Answer key

  1. 1. B · The number of edges from the root to that node
  2. 2. B · It stores elements without duplicates
  3. 3. D · Null (no node)
  4. 4. B · 1 2 4 5 3
  5. 5. B · A balanced binary search tree
  6. 6. C · 2h+1 − 1
  7. 7. A · 60
  8. 8. C · A max-heap of size k, replacing its root when a smaller value arrives

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