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 does the peek (or top) operation on a stack do?
- AReturns the bottom element of the stack
- BRemoves the top element and returns it
- CReturns the top element without removing it
- DRemoves all elements from the stack
2.Easy
In which order does a preorder traversal of a binary tree visit nodes?
- ALeft subtree, then right subtree, then node
- BNode, then left subtree, then right subtree
- CLeft subtree, then node, then right subtree
- DLevel by level from the top
3.Easy
What is a directed graph?
- AA graph in which every pair of distinct vertices is joined by an edge
- BA graph whose edges have a direction from one vertex to another
- CA graph that has no cycles
- DA graph whose edges all have weights
4.Medium
How many nodes does a perfect binary tree of height h have, with height counted in edges?
- A2h
- B2h+1 − 1
- Ch2
- D2h + 1
5.Medium
A hash table of size 7 uses h(k) = k mod 7 and linear probing. After inserting 10, where is 17 stored?
- AIndex 6
- BIndex 3
- CIndex 4
- DIndex 0
6.Medium
In a hash table using open addressing, what is the maximum possible load factor?
- A1
- B0.5
- C2
- DThere is no maximum
7.Hard
Which data structure supports an LRU (least recently used) cache with O(1) get and put?
- AA hash map combined with a doubly linked list
- BA stack only
- CA single sorted array
- DA binary search tree only
8.Hard
An object is stored as a key in a hash map, and then a field used by its hash function is changed. What is the likely result?
- AThe map throws an error at the moment the field changes
- BLooking up the object may fail because it sits in the bucket for its old hash
- CNothing changes, because a key's hash is computed only once when the program starts
- DThe map automatically moves the key to its new bucket