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

How many edges does a tree with n nodes have?

  1. An − 1
  2. Bn
  3. C2n
  4. Dn + 1
2.Easy

Which structure is typically used to implement a priority queue?

  1. AA stack
  2. BA string
  3. CA singly linked list without order
  4. DA heap
3.Easy

What does a map (dictionary) store?

  1. AOnly values in insertion order
  2. BValues sorted by size
  3. CPairs of equal keys
  4. DKey-value pairs with unique keys
4.Medium

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

  1. AFollow left children from the root until there is no left child
  2. BLook at the root only
  3. CFollow right children from the root until there is no right child
  4. DVisit every leaf and compare them
5.Medium

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

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

Why can a binary heap be stored in an array without any pointers?

  1. AArrays automatically keep the keys in sorted order, so no pointers are needed
  2. BHeaps never contain more than 100 elements
  3. CIt is a complete binary tree, so positions follow simple index formulas
  4. DAll keys in a heap are equal
7.Hard

In a hash table using open addressing, why is deletion often done with a "tombstone" marker?

  1. ATombstones speed up hashing
  2. BEmptying the slot could break the probe sequence for other keys
  3. CIt makes the table sorted
  4. DDeleted keys must be kept forever for security, so slots can never be reused
8.Hard

For a hash table to work correctly, which rule must hash and equality follow?

  1. AEqual hash values imply equal keys
  2. BEqual keys must have equal hash values
  3. CHash values must be prime numbers
  4. DDifferent keys must have different hash values

Answer key

  1. 1. A · n − 1
  2. 2. D · A heap
  3. 3. D · Key-value pairs with unique keys
  4. 4. A · Follow left children from the root until there is no left child
  5. 5. C · O(n), because later elements must shift
  6. 6. C · It is a complete binary tree, so positions follow simple index formulas
  7. 7. B · Emptying the slot could break the probe sequence for other keys
  8. 8. B · Equal keys must have equal hash values

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