Skip to content

Data Structures final exam review

The topics, worked on a live board with an AI tutor.

Units

  1. Analysis of algorithms

    Practice

    How many steps does sum_list take on a list of 6 numbers?Answer

    20

    Hint: 3 × 6 + 2

    Counting steps
    Find c and n₀ that show 2n + 10 is O(n).Answer

    c = 3 and n₀ = 10, since 2n + 10 ≤ 3n means n ≥ 10

    Hint: Choose c = 3 and solve for n

    Big-O, Big-Omega and Big-Theta
  2. Linked lists

    Practice

    Build, in the same way, the chain 2, 7 with head referring to the node holding 2. Which Node calls do you write?Answer

    c = Node(7), then head = Node(2, c)

    Hint: Start from the last node

    Singly linked lists
    Starting from 2 → 4 → 9 → None, run insert_after on the node holding 9 with x = 1. What is the list, and what is the next of the new node?Answer

    2 → 4 → 9 → 1 → None; the next of the new node is None

    Hint: Line 3 copies the next of the node 9

    Linked list insertion and deletion
  3. Stacks and queues

    Practice

    A stack gets push(6), push(1), push(4), then pop(), then push(5). What does peek return, and what is the whole stack from bottom to top?Answer

    5; the stack is 6, 1, 5

    Hint: Pop() removes the 4

    Stacks
    A queue gets enqueue(p), enqueue(q), dequeue(), enqueue(r), dequeue(). What does the second dequeue return, and what is left in the queue?Answer

    q; the queue holds only r

    Hint: p leaves first, then q

    Queues
  4. Recursion

    What exams ask

    Search problems such as filling a sudoku, finding a route that visits every town or checking every way to schedule exams are solved by trying a choice, going on, and undoing it when it fails, and the same pattern also shows when a search is too big to finish.

    Practice

    For the list 5, 1, what does total return, how many calls are made, and how deep does the stack get?Answer

    6; 3 calls; 3 frames

    Hint: One call for each node and one for None

    Recursion and the call stack
    The same search on a list of 32 numbers. Using the recurrence, what are T(32) and the sizes of what is left after each probe?Answer

    T(32) = 6; the sizes go 32, 16, 8, 4, 2, 1

    Hint: Build up from T(1) = 1 by adding 1

    Recurrences and divide and conquer
  5. Sorting

    Practice

    Selection sort on 4, 7, 1, 9, 3. What is the list after the first two passes, and how many comparisons in all?Answer

    1, 3, 4, 9, 7; 10 comparisons

    Hint: Pass 0 swaps the 4 and the 1, and the total is 4 + 3 + 2 + 1

    Selection and insertion sort
    Merge 2, 9, 10 with 4, 5, 11, 12. What is the output, and how many comparisons were made?Answer

    2, 4, 5, 9, 10, 11, 12; 5 comparisons

    Hint: After the 10 is taken the first list is empty, and the rest is copied

    Merge sort
  6. Trees

    Practice

    In the same tree, what are the depth and the height of the node 14?Answer

    Depth 2 and height 1

    Hint: 14 is below 10, which is below 8, and 13 is one line below 14

    Trees and binary trees
    What is the preorder of the tree with root 6, whose left child 3 has the children 2 and 5, and whose right child is 8?Answer

    6, 3, 2, 5, 8

    Hint: Add 6, then do all of the subtree under 3, then 8

    Tree traversals
  7. Heaps and priority queues

    Practice

    In the array 2, 5, 3, 8, 6, 4, 9, which indices are the children of index 1, and which index is the parent of index 6?Answer

    The children are at 3 and 4, holding 8 and 6, and the parent of index 6 is at 2, holding 3

    Hint: 2i + 1, 2i + 2 and (i − 1)//2

    Binary heaps and insertion
    Pop from the min-heap 1, 3, 2, 7, 4. What does the pop return, and what is the array afterwards?Answer

    1, and the array is 2, 3, 4, 7

    Hint: 4 goes to the root and swaps with the smaller child, 2

    Removing the minimum
  8. Hash tables

    What exams ask

    Hash tables give Python's dict and set, database indexes and caches their average constant-time lookups, and choosing a hash and a table size that spread keys evenly is what keeps them fast; computing bucket indexes by hand is a standard exam question.

    Separate chaining is the simplest working hash table and the model behind Java's HashMap; the load factor argument, a successful search about 1 + α/2 and a failed one about α, and doubling to keep α bounded are what exams and interviews ask when they ask why hash tables are O(1).

    Practice

    With m = 7, place 14, 21 and 9. Which of them collide?Answer

    14 % 7 = 0 and 21 % 7 = 0 collide in bucket 0; 9 goes to bucket 2

    Hint: Multiples of 7 give 0

    Hash functions and hash tables
    Suppose the table never grew. Insert 15 and 20 into the table above. Which chain do they join, and what does a search for 20 cost?Answer

    Bucket 0, making the chain 25, 40, 15, 20; a search for 20 takes 4 comparisons

    Hint: Both are multiples of 5

    Collisions: separate chaining
  9. Graphs

    What exams ask

    BFS finds the fewest hops in an unweighted network, such as degrees of separation, the fewest moves in a puzzle or the shortest route in a grid maze, and it is the skeleton of many graph algorithms; tracing its queue is a standard exam question.

    DFS is the other basic graph walk; it explores mazes, finds what is reachable, splits a network into connected pieces, detects cycles such as circular dependencies, and underlies topological sort, all in O(V + E); tracing its order is a staple exam question next to BFS.

    Practice

    A graph has degrees 1, 2, 2, 3 and 4. How many edges does it have?Answer

    6, since the degrees add to 12

    Hint: Halve the sum

    Graphs and their representations
    Run BFS from F. What are the distances of E, C and A?Answer

    1, 2 and 3

    Hint: F's only neighbour is E

    Breadth-first search

Questions

What does the Data Structures review cover?

Every unit of the course, 9 in all. Pick one and Helio teaches it on the board, then asks you questions. Analysis of algorithms; Linked lists; Stacks and queues; Recursion; Sorting; Trees; Heaps and priority queues; Hash tables; Graphs.

Can it help the week before my final?

Yes. Pick a topic, and Helio explains it, checks you and goes over what you miss.

Is it the same as my class?

It follows the usual topics of Data Structures. For anything else on your syllabus, type the topic and Helio teaches that too.

How much is free?

30 minutes a month for one learner, with no card. Starter is free for 7 days with a card: cancel in time and pay nothing.

A tutor you can talk to

30 minutes every month. No card.