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 graph, what is a cycle?

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

What is a weighted graph?

  1. AA graph in which each edge has a numeric value such as a cost or distance
  2. BA graph in which some vertices are heavier than others because they have more edges
  3. CA graph that is stored on disk
  4. DA graph with more edges than vertices
3.Easy

What is the defining feature of a set as an abstract data type?

  1. AIt stores elements without duplicates
  2. BIt stores elements in sorted order
  3. CIt stores key-value pairs
  4. DIt allows access only at one end
4.Medium

An application needs fast lookups and also needs to list keys in sorted order and answer range queries. Which structure fits best?

  1. AA stack
  2. BA hash table
  3. CA balanced binary search tree
  4. DAn unsorted linked list
5.Medium

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

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

How long does it take to check whether the edge (u, v) exists using an adjacency matrix?

  1. AO(1)
  2. BO(E)
  3. CO(V)
  4. DO(log V)
7.Hard

The keys 5, 3, 8, 1 are inserted in this order into an empty array-based binary min-heap. What is the final array?

  1. A[1, 5, 8, 3]
  2. B[3, 1, 8, 5]
  3. C[1, 3, 5, 8]
  4. D[1, 3, 8, 5]
8.Hard

How can you detect whether a singly linked list contains a cycle using O(1) extra memory?

  1. AStore every visited node in a hash set
  2. BMove one pointer by one step and another by two steps and check whether they meet
  3. CCount the nodes until the count exceeds the list length field
  4. DSort the list and look for duplicates

Answer key

  1. 1. B · A path that starts and ends at the same vertex
  2. 2. A · A graph in which each edge has a numeric value such as a cost or distance
  3. 3. A · It stores elements without duplicates
  4. 4. C · A balanced binary search tree
  5. 5. B · O(n), because later elements must shift
  6. 6. A · O(1)
  7. 7. D · [1, 3, 8, 5]
  8. 8. B · Move one pointer by one step and another by two steps and check whether they meet

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