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 a tree, what are sibling nodes?

  1. ANodes that share the same parent
  2. BNodes that have no children
  3. CNodes at the same depth in different subtrees
  4. DA node and its parent
2.Easy

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

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

How are the elements of an array stored in memory?

  1. AScattered, linked by pointers
  2. BIn random locations chosen by a hash
  3. CIn a tree of memory pages
  4. DIn one contiguous block, one after another
4.Medium

Which data structure is used to check whether the brackets in "{[()]}" are balanced?

  1. AA stack
  2. BA queue
  3. CA binary search tree
  4. DA hash table
5.Medium

How does extract-min work in a binary min-heap?

  1. ARemove the root and shift all elements one place left
  2. BSearch all leaves for the new minimum
  3. CRemove the last element of the array
  4. DMove the last element to the root and sift it down
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(1)
  2. BO(n²)
  3. CO(n)
  4. DO(log n)
7.Hard

A segment tree is typically used to:

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

Why are B-trees used in databases and file systems?

  1. ATheir high branching factor keeps the tree shallow, reducing slow disk reads
  2. BThey use less memory than any array, so the entire index always fits in main memory
  3. CThey store data unsorted
  4. DThey need no rebalancing

Answer key

  1. 1. A · Nodes that share the same parent
  2. 2. A · The number of edges connected to it
  3. 3. D · In one contiguous block, one after another
  4. 4. A · A stack
  5. 5. D · Move the last element to the root and sift it down
  6. 6. A · O(1)
  7. 7. A · Answer range queries (such as range sums) and updates in O(log n)
  8. 8. A · Their high branching factor keeps the tree shallow, reducing slow disk reads

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