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 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. CNode, then left subtree, then right subtree
  4. DLeft subtree, then node, then right subtree
2.Easy

The depth of a node in a tree is:

  1. AThe number of edges from that node to the deepest leaf
  2. BThe number of edges from the root to that node
  3. CThe number of children of that node
  4. DThe number of nodes in its subtree
3.Easy

What does a map (dictionary) store?

  1. AValues sorted by size
  2. BPairs of equal keys
  3. COnly values in insertion order
  4. DKey-value pairs with unique keys
4.Medium

How is a new element inserted into a binary heap?

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

A road network has 100,000 junctions, each connected to about 4 others. Which representation is most suitable?

  1. AA single sorted array of vertices
  2. BAdjacency list
  3. CA binary heap of edges
  4. DAdjacency matrix
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

How long does it take to search for an arbitrary key in a binary min-heap of n elements?

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

In a circular buffer, front == rear can mean either empty or full. What is a common way to tell them apart?

  1. AKeep a separate element count, or always leave one slot unused
  2. BStore the front and rear in the same variable
  3. CAlways clear the buffer whenever front equals rear, so the state is empty
  4. DMake the buffer size a power of two

Answer key

  1. 1. B · Left subtree, then right subtree, then node
  2. 2. B · The number of edges from the root to that node
  3. 3. D · Key-value pairs with unique keys
  4. 4. A · Place it at the end of the array and sift it up while it violates the heap property
  5. 5. B · Adjacency list
  6. 6. D · O(1)
  7. 7. C · O(n)
  8. 8. A · Keep a separate element count, or always leave one slot unused

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