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 directed graph?

  1. AA graph whose edges all have weights
  2. BA graph whose edges have a direction from one vertex to another
  3. CA graph in which every pair of distinct vertices is joined by an edge
  4. DA graph that has no cycles
2.Easy

What is the name of the stack operation that adds an element to the top?

  1. APop
  2. BPush
  3. CPeek
  4. DEnqueue
3.Easy

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

  1. AQueue
  2. BBinary tree
  3. CStack
  4. DHeap
4.Medium

How is a new element inserted into a binary heap?

  1. ARebuild the whole heap from scratch
  2. BPlace it at the end of the array and sift it up while it violates the heap property
  3. CInsert it at its sorted position by shifting all of the larger elements one place right
  4. DPlace it at the root and sift it down
5.Medium

What is the time complexity of searching for a key in a balanced binary search tree?

  1. AO(n)
  2. BO(log n)
  3. CO(n log n)
  4. DO(1)
6.Medium

In a binary min-heap stored in an array (index starting at 0), the children of the node at index i are at:

  1. Ai/2 and i/2 + 1
  2. Bi + 1 and i + 2
  3. C2i and 2i + 1
  4. D2i + 1 and 2i + 2
7.Hard

The time complexity of decrease-key in a binary heap (given the element's position) is:

  1. AO(log n)
  2. BO(1)
  3. CO(n)
  4. DO(n log n)
8.Hard

Why do databases often use B+ trees rather than plain B-trees for indexes?

  1. AAll records are in linked leaves, which makes range scans efficient
  2. BB+ trees do not need to be balanced
  3. CB+ trees use binary internal nodes, which reduces the number of disk reads
  4. DB+ trees store data only in the root

Answer key

  1. 1. B · A graph whose edges have a direction from one vertex to another
  2. 2. B · Push
  3. 3. A · Queue
  4. 4. B · Place it at the end of the array and sift it up while it violates the heap property
  5. 5. B · O(log n)
  6. 6. D · 2i + 1 and 2i + 2
  7. 7. A · O(log n)
  8. 8. A · All records are in linked leaves, which makes range scans efficient

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