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 the root of a tree?

  1. AThe node with the largest value
  2. BThe top node with no parent
  3. CAny node in the middle
  4. DA node with no children
2.Easy

In a singly linked list, what does the next pointer of the last node contain?

  1. ANull (no node)
  2. BThe address of the first node
  3. CThe number of nodes in the list
  4. DThe address of the previous node
3.Easy

What does the peek (or top) operation on a stack do?

  1. AReturns the top element without removing it
  2. BRemoves the top element and returns it
  3. CRemoves all elements from the stack
  4. DReturns the bottom element of the stack
4.Medium

A trie (prefix tree) is especially efficient for:

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

What is true of the adjacency matrix of an undirected graph?

  1. AIts main diagonal always contains ones
  2. BIt is symmetric about the main diagonal
  3. CIt is always upper triangular
  4. DEach row has exactly one 1
6.Medium

A queue is implemented with two stacks: push onto an "in" stack, and pop from an "out" stack, refilling it from "in" when empty. What is the amortised cost per operation?

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

Under simple uniform hashing, what is the expected time of a search in a hash table with chaining, n keys and m buckets?

  1. AO(1 + α), where α = n / m
  2. BO(m)
  3. CO(n)
  4. DO(log n)
8.Hard

Which statement about heapsort is correct?

  1. AIt needs a binary search tree as extra storage, so it uses O(n) extra memory
  2. BIt runs in O(n) time on any input
  3. CIt is stable and needs O(n) extra memory
  4. DIt sorts in place in O(n log n) worst-case time but is not stable

Answer key

  1. 1. B · The top node with no parent
  2. 2. A · Null (no node)
  3. 3. A · Returns the top element without removing it
  4. 4. B · Prefix searches such as autocomplete
  5. 5. B · It is symmetric about the main diagonal
  6. 6. D · O(1)
  7. 7. A · O(1 + α), where α = n / m
  8. 8. D · It sorts in place in O(n log n) worst-case time but is not stable

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