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 is a deque (double-ended queue)?

  1. AA structure that allows insertion and removal at both ends
  2. BA queue sorted by priority
  3. CTwo separate queues sharing one array
  4. DA queue that only allows removal at the back
2.Easy

The height of a tree is commonly defined as:

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

Which operation is O(1) for an array but O(n) for a singly linked list?

  1. AAccessing the element at index i
  2. BInserting at the head
  3. CRemoving the head element
  4. DReading the first element
4.Medium

A trie (prefix tree) is especially efficient for:

  1. APrefix searches such as autocomplete
  2. BStoring matrices
  3. CSorting numbers
  4. DFinding shortest paths
5.Medium

How does extract-min work in a binary min-heap?

  1. ARemove the last element of the array
  2. BRemove the root and shift all elements one place left
  3. CMove the last element to the root and sift it down
  4. DSearch all leaves for the new minimum
6.Medium

In a circular queue stored in an array of capacity n, how is the next position after index i computed?

  1. Ai + 1
  2. B2i + 1
  3. C(i − 1) mod n
  4. D(i + 1) mod n
7.Hard

A dynamic array doubles when full. Why do many implementations shrink it only when it is one quarter full, not half full?

  1. ATo keep the array sorted
  2. BBecause halving at one half would lose elements
  3. CTo avoid repeated resizing when pushes and pops alternate near the boundary
  4. DBecause the memory allocator can only release memory in quarters of a block
8.Hard

What guarantee do the red-black tree rules give about the height of a tree with n keys?

  1. AThe height is at most n / 2
  2. BThe height is exactly log2 n
  3. CThe height is at most 2 log2(n + 1)
  4. DThe height is at most log2 n + 1

Answer key

  1. 1. A · A structure that allows insertion and removal at both ends
  2. 2. B · The number of edges on the longest path from the root to a leaf
  3. 3. A · Accessing the element at index i
  4. 4. A · Prefix searches such as autocomplete
  5. 5. C · Move the last element to the root and sift it down
  6. 6. D · (i + 1) mod n
  7. 7. C · To avoid repeated resizing when pushes and pops alternate near the boundary
  8. 8. C · The height is at most 2 log2(n + 1)

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