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

In an undirected graph, what is the degree of a vertex?

  1. AThe length of the longest path from it
  2. BIts position in the adjacency list
  3. CThe number of edges connected to it
  4. DThe number of vertices in the graph
2.Easy

In a graph, what is a cycle?

  1. AA vertex with no edges
  2. BA path that visits every vertex
  3. CA path that starts and ends at the same vertex
  4. DAn edge with a weight of zero
3.Easy

What does a hash function do in a hash table?

  1. ACounts how many keys are stored
  2. BEncrypts the values in the table
  3. CMaps a key to an index in the table
  4. DSorts the keys in the table
4.Medium

A hash table of size 7 uses h(k) = k mod 7 and linear probing. After inserting 10, where is 17 stored?

  1. AIndex 4
  2. BIndex 0
  3. CIndex 3
  4. DIndex 6
5.Medium

The keys 50, 30, 70, 20, 40 are inserted in this order into an empty binary search tree. What is the left child of 30?

  1. A70
  2. B50
  3. C20
  4. D40
6.Medium

Which abstract data type does Dijkstra's algorithm use to choose the next vertex to process?

  1. AA stack
  2. BA min-priority queue
  3. CA set with no ordering
  4. DA FIFO queue
7.Hard

A Fenwick tree (binary indexed tree) over an array of n numbers supports which operations in O(log n)?

  1. APrefix-sum queries and single-element updates
  2. BFinding the median in O(1)
  3. CSorting the array and binary search
  4. DInserting elements at any position
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 min-heap holding all n values, from which the k smallest are popped at the end
  2. BA FIFO queue of the last k values
  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. C · The number of edges connected to it
  2. 2. C · A path that starts and ends at the same vertex
  3. 3. C · Maps a key to an index in the table
  4. 4. A · Index 4
  5. 5. C · 20
  6. 6. B · A min-priority queue
  7. 7. A · Prefix-sum queries and single-element updates
  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.