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

A graph consists of:

  1. AOnly a root and leaves
  2. BKeys and hashes only
  3. CRows and columns
  4. DVertices (nodes) connected by edges
2.Easy

In a hash table, what is a collision?

  1. AA key cannot be hashed at all
  2. BTwo different keys map to the same index
  3. CTwo equal keys are stored twice
  4. DThe table runs out of memory
3.Easy

In which order does a postorder traversal of a binary tree visit nodes?

  1. ARight subtree, then node, then left subtree
  2. BLeft subtree, then right subtree, then node
  3. CLeft subtree, then node, then right subtree
  4. DNode, then left subtree, then right subtree
4.Medium

An adjacency matrix for a graph with V vertices uses how much memory?

  1. AO(log V)
  2. BO(V + E)
  3. CO(E)
  4. DO(V²)
5.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(1)
  3. CO(n²)
  4. DO(log n)
6.Medium

Which data structure is used by depth-first search (iterative version)?

  1. AA stack
  2. BA queue
  3. CA heap
  4. DA hash table only
7.Hard

A segment tree is typically used to:

  1. AImplement a stack
  2. BAnswer range queries (such as range sums) and updates in O(log n)
  3. CSort strings alphabetically by storing each prefix in a separate tree node
  4. DStore a graph's edges
8.Hard

An object is stored as a key in a hash map, and then a field used by its hash function is changed. What is the likely result?

  1. AThe map throws an error at the moment the field changes
  2. BLooking up the object may fail because it sits in the bucket for its old hash
  3. CThe map automatically moves the key to its new bucket
  4. DNothing changes, because a key's hash is computed only once when the program starts

Answer key

  1. 1. D · Vertices (nodes) connected by edges
  2. 2. B · Two different keys map to the same index
  3. 3. B · Left subtree, then right subtree, then node
  4. 4. D · O(V²)
  5. 5. B · O(1)
  6. 6. A · A stack
  7. 7. B · Answer range queries (such as range sums) and updates in O(log n)
  8. 8. B · Looking up the object may fail because it sits in the bucket for its old hash

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