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

Which data structure is most suitable for the "undo" feature in a text editor?

  1. AGraph
  2. BQueue
  3. CStack
  4. DHash set
2.Easy

The height of a tree is commonly defined as:

  1. AThe number of edges on the longest path from the root to a leaf
  2. BThe number of children of the root
  3. CThe total number of nodes in the tree, counting the root and every leaf
  4. DThe number of leaves in the tree
3.Easy

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

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

How do you find the minimum key in a binary search tree?

  1. ALook at the root only
  2. BFollow left children from the root until there is no left child
  3. CVisit every leaf and compare them
  4. DFollow right children from the root until there is no right child
5.Medium

A node in a binary search tree has a right subtree. Where is its in-order successor?

  1. AThe leftmost node of its right subtree
  2. BIts parent, always
  3. CThe rightmost node of its left subtree
  4. DIts right child, always
6.Medium

What is the worst-case height of a binary search tree with n nodes if keys are inserted in sorted order?

  1. A1
  2. Bn (it becomes a linked list)
  3. Clog₂ n
  4. D√n
7.Hard

A segment tree is typically used to:

  1. AImplement a stack
  2. BSort strings alphabetically by storing each prefix in a separate tree node
  3. CAnswer range queries (such as range sums) and updates in O(log n)
  4. DStore a graph's edges
8.Hard

In Kruskal's minimum spanning tree algorithm, what is the union-find structure used for?

  1. AChecking whether an edge's two endpoints are already connected
  2. BChoosing the starting vertex
  3. CSorting the edges by weight
  4. DStoring each vertex's current distance from the source during the search

Answer key

  1. 1. C · Stack
  2. 2. A · The number of edges on the longest path from the root to a leaf
  3. 3. C · It stores elements without duplicates
  4. 4. B · Follow left children from the root until there is no left child
  5. 5. A · The leftmost node of its right subtree
  6. 6. B · n (it becomes a linked list)
  7. 7. C · Answer range queries (such as range sums) and updates in O(log n)
  8. 8. A · Checking whether an edge's two endpoints are already connected

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