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. ARows and columns
  2. BVertices (nodes) connected by edges
  3. CKeys and hashes only
  4. DOnly a root and leaves
2.Easy

Where is the smallest element of a binary min-heap found?

  1. AIn any leaf, which must be searched
  2. BAt the root
  3. CIn the leftmost leaf
  4. DIn the last array position
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 one contiguous block, one after another
  4. DIn a tree of memory pages
4.Medium

What is the cost of inserting an element in the middle of an array of n elements?

  1. AO(n), because later elements must shift
  2. BO(n²), because each element moves n times
  3. CO(1), because the index is known
  4. DO(log n), because of binary search
5.Medium

What is the purpose of a sentinel (dummy) node in a linked list?

  1. ATo prevent the list from growing beyond a fixed maximum number of nodes
  2. BTo make the list sorted
  3. CTo store the number of elements
  4. DTo simplify code by removing special cases at the head or tail
6.Medium

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

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

Why do databases often use B+ trees rather than plain B-trees for indexes?

  1. AB+ trees do not need to be balanced
  2. BAll records are in linked leaves, which makes range scans efficient
  3. CB+ trees store data only in the root
  4. DB+ trees use binary internal nodes, which reduces the number of disk reads
8.Hard

A Fenwick tree (binary indexed tree) over an array of n numbers supports which operations in O(log n)?

  1. AFinding the median in O(1)
  2. BInserting elements at any position
  3. CPrefix-sum queries and single-element updates
  4. DSorting the array and binary search

Answer key

  1. 1. B · Vertices (nodes) connected by edges
  2. 2. B · At the root
  3. 3. C · In one contiguous block, one after another
  4. 4. A · O(n), because later elements must shift
  5. 5. D · To simplify code by removing special cases at the head or tail
  6. 6. D · A stack
  7. 7. B · All records are in linked leaves, which makes range scans efficient
  8. 8. C · Prefix-sum queries and single-element updates

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