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 name of the stack operation that adds an element to the top?

  1. APush
  2. BPeek
  3. CPop
  4. DEnqueue
2.Easy

In a graph, what is a cycle?

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

In a hash table, what is a collision?

  1. AThe table runs out of memory
  2. BTwo different keys map to the same index
  3. CTwo equal keys are stored twice
  4. DA key cannot be hashed at all
4.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(log n)
  4. DO(n)
5.Medium

Why is traversing an array often much faster in practice than traversing a linked list of the same length?

  1. AArray elements are contiguous, so they make good use of the CPU cache
  2. BLinked lists always store elements on disk
  3. CArrays use fewer instructions to compare values
  4. DArray traversal is O(1) in total, while linked list traversal is O(n)
6.Medium

A node in a binary search tree has a right subtree. Where is its in-order successor?

  1. AIts parent, always
  2. BIts right child, always
  3. CThe rightmost node of its left subtree
  4. DThe leftmost node of its right subtree
7.Hard

Why might a server choose its hash function at random from a universal family?

  1. ATo guarantee that no two keys can ever collide, whatever keys an attacker sends
  2. BTo remove the need to resize the table
  3. CTo make the hash table sorted
  4. DTo make it very unlikely that an attacker can pick many keys that all collide
8.Hard

Inserting a key into an AVL tree makes a node's left subtree too tall, and the new key went into the left subtree of its left child (left-left case). Which fix is applied?

  1. AA left rotation at the child, then a right rotation at the node
  2. BA single right rotation at the unbalanced node
  3. CRecolouring the node and its child
  4. DA single left rotation at the unbalanced node

Answer key

  1. 1. A · Push
  2. 2. A · A path that starts and ends at the same vertex
  3. 3. B · Two different keys map to the same index
  4. 4. B · O(1)
  5. 5. A · Array elements are contiguous, so they make good use of the CPU cache
  6. 6. D · The leftmost node of its right subtree
  7. 7. D · To make it very unlikely that an attacker can pick many keys that all collide
  8. 8. B · A single right rotation at the unbalanced node

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