University

Data Structures Practice Exam — Free

Generate a realistic AI practice exam with worked solutions

Generate Your Free Practice Exam

Data Structures examines arrays, linked lists, stacks, queues, trees, heaps, hash tables, and graphs, along with the algorithms that operate on them. Exams blend code-tracing questions with complexity analysis and design problems. Practice exams help you move fluently between pseudocode, implementation, and Big-O reasoning.

Crameleon generates your practice exam from your own course materials — lecture notes, syllabi, past exams, or any combination. Upload what you have and get a fully formatted exam with the same structure as your professor's actual tests: multiple choice, short answer, and worked problems, all with step-by-step solutions (first 2 free; all with Premium).

Unlike generic question banks, every Crameleon exam reflects the specific framing, terminology, and difficulty level of your course. You'll practise answering questions that look and feel like the real thing — which is exactly what the research on retrieval practice says you should be doing. Students who test themselves with practice exams consistently outperform students who re-read notes, regardless of study time. The generation takes under 2 minutes and your first exam is completely free.

Topics typically tested

  • Arrays, linked lists & amortized analysis
  • Stacks, queues & deques
  • Binary search trees & AVL trees
  • Heaps & priority queues
  • Hash tables & collision resolution
  • Graph traversals (BFS, DFS) & shortest paths

Sample Data Structures exam questions

Three original questions in the style of a real Data Structures exam, each with a worked solution. Your own practice exam can be built from your course's notes and syllabus.

Question 1Multiple choiceHeaps & priority queues

The keys 12, 7, 20, 3, 25, 9, 14 are inserted one at a time, in that order, into an initially empty binary max-heap stored in an array with the root at index 0 (the children of index ii are at indices 2i+12i+1 and 2i+22i+2). Each insertion appends the key at the end of the array and sifts it up. What is the array after all seven insertions?

  1. A.

    25, 20, 14, 3, 7, 9, 12

  2. B.

    25, 12, 20, 3, 7, 9, 14

  3. C.

    25, 20, 14, 12, 9, 7, 3

  4. D.

    3, 7, 9, 12, 25, 20, 14

Worked solution
Answer:A

Sift-up compares the new key with its parent at index ⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor and swaps while the key is larger than the parent.

  • Insert 12: [12].
  • Insert 7 at index 1; parent 12 is larger, stop: [12, 7].
  • Insert 20 at index 2; parent 12 is smaller, swap: [20, 7, 12].
  • Insert 3 at index 3; parent (index 1) is 7, larger, stop: [20, 7, 12, 3].
  • Insert 25 at index 4; parent 7 is smaller, swap to index 1; parent 20 is smaller, swap to the root: [25, 20, 12, 3, 7].
  • Insert 9 at index 5; parent (index 2) is 12, larger, stop: [25, 20, 12, 3, 7, 9].
  • Insert 14 at index 6; parent 12 is smaller, swap to index 2; parent 25 is larger, stop: [25, 20, 14, 3, 7, 9, 12].

Verify the heap property on the result: 25 is at least 20 and 14; 20 is at least 3 and 7; 14 is at least 9 and 12. Every parent dominates its children and the array is a complete tree, so this is a valid max-heap.

Option B is the tempting one: it is what the linear-time bottom-up build-heap produces when applied to the whole array [12, 7, 20, 3, 25, 9, 14] at once. It is also a valid max-heap, but the question specifies repeated insertion, and the two procedures generally give different arrays. Option C is the keys in descending order, which sift-up never produces here (7 and 3 never move once placed). Option D is a min-heap.

[25, 20, 14, 3, 7, 9, 12] (option A).

Question 2Short answerHash tables & collision resolution

A hash table has 11 slots, indexed 0 to 10, and uses the hash function h(k)=k mod 11h(k) = k \bmod 11 with linear probing (step 1, wrapping from slot 10 back to slot 0) to resolve collisions. The keys 23, 14, 34, 45 and 12 are inserted in that order into the empty table. Give the slot where each key is stored, and the number of slots examined when key 12 is subsequently searched for.

Worked solution
Answer:

23 → slot 1, 14 → slot 3, 34 → slot 2, 45 → slot 4, 12 → slot 5; searching for 12 examines 5 slots

Compute each home slot with h(k)=k mod 11h(k) = k \bmod 11 and probe forward from it until an empty slot appears.

  • 23: 23 mod 11=123\bmod 11 = 1; slot 1 is empty, store there.
  • 14: 14 mod 11=314\bmod 11 = 3; slot 3 is empty, store there.
  • 34: 34 mod 11=134\bmod 11 = 1; slot 1 holds 23, so probe slot 2, which is empty: store at 2.
  • 45: 45 mod 11=145\bmod 11 = 1; slots 1, 2 and 3 are occupied; slot 4 is empty: store at 4.
  • 12: 12 mod 11=112\bmod 11 = 1; slots 1, 2, 3 and 4 are occupied; slot 5 is empty: store at 5.

Final table (slot: key): 1: 23, 2: 34, 3: 14, 4: 45, 5: 12, with all other slots empty.

A search for 12 retraces its insertion path: start at slot 1 and compare with 23, 34, 14 and 45, then find 12 at slot 5. That is 5 slots examined. Check by counting positions: slot 5 is 4 places past the home slot 1, so the probe count is 4+1=54+1 = 5.

Four of the five keys hash to slot 1, which is why they form one contiguous run from slot 1 to slot 5. This is primary clustering, the main weakness of linear probing: the run grows and every key that lands in it pays for the whole run. The load factor is 5/11≈0.455/11 \approx 0.45, and an unsuccessful search for a key with home slot 1 (such as 56) would examine slots 1 through 6, six probes, before reaching an empty slot.

23 → 1, 14 → 3, 34 → 2, 45 → 4, 12 → 5; the search for 12 examines 5 slots.

Question 3Worked problemBinary search trees & AVL trees

Insert the keys 50, 30, 70, 20, 40, 10, 55, 65, in that order, into an initially empty AVL tree. (a) Identify every insertion that makes the tree unbalanced, the node at which the rotation is performed, and whether a single or double rotation is used. (b) Give the preorder traversal of the final tree and its height, measured as the number of edges on the longest path from the root to a leaf.

Worked solution
Answer:

(a) Inserting 10 unbalances 50 (left-left case): single right rotation at 50. Inserting 65 unbalances 70 (left-right case): double rotation, a left rotation at 55 followed by a right rotation at 70. (b) Preorder 30, 20, 10, 50, 40, 65, 55, 70; height 3

Balance factor = height(left subtree) − height(right subtree); every node must have balance −1, 0 or 1, and rebalancing happens at the lowest unbalanced ancestor of the new node. In the balance checks below, heights count nodes (an empty subtree has height 0, a single node height 1); the differences are the same under the edge count used for part (b).

Insert 50, 30, 70: root 50 with children 30 and 70, balanced. Insert 20 (left of 30) and 40 (right of 30): node 50 has balance +1, still fine.

Insert 10 (left of 20). Node 50 now has left height 3 and right height 1, balance +2. The new key lies in the left subtree of 50's left child, a left-left case, so a single right rotation at 50 fixes it: 30 becomes the root, 50 becomes its right child, and 30's former right child 40 becomes 50's left child. Tree: 30 with children 20 (left child 10) and 50 (children 40, 70).

Insert 55: right of 30, right of 50, left of 70. Balances: 70 is +1, 50 is −1, 30 is −1. No rotation.

Insert 65: right of 55. Node 70 now has left height 2 and right height 0, balance +2. The new key is in the right subtree of 70's left child, a left-right case, so a double rotation is needed: a left rotation at 55 (65 moves up, 55 becomes its left child), then a right rotation at 70 (65 becomes the subtree root with children 55 and 70). Afterwards 50 has children 40 and 65 with balance −1, and the root 30 has balance −1.

Final tree: 30 has left child 20 (with left child 10) and right child 50; 50 has children 40 and 65; 65 has children 55 and 70. Preorder (node, left, right): 30, 20, 10, 50, 40, 65, 55, 70. The longest root-to-leaf path is 30–50–65–55 (or 30–50–65–70), 3 edges. Check: the in-order traversal 10, 20, 30, 40, 50, 55, 65, 70 is sorted, so the BST property holds, and every balance factor lies within −1 to 1.

(a) Single right rotation at 50 after inserting 10; double rotation (left at 55, then right at 70) after inserting 65. (b) Preorder 30, 20, 10, 50, 40, 65, 55, 70; height 3.

Used by 3,000+ students across Canadian universities·Step-by-step solutions (first 2 free)·Works for any course

Ready to practice?

Generate Your Free Data Structures Practice Exam

Your first exam is free — just sign in, no credit card.