Data Structures

Overview: Final Exam


  1. Algorithm Basics. [STEPHENS] chapter 1.
    1. Basic concepts (algorithms, data structures, pseudocode).
    2. Big O notation.
    3. Common runtime functions: \(O(1)\), \(O(\log N)\), \(O(N)\), \(O(N \log N)\), \(O(N^2)\), \(O(N^3)\), \(O(2^N)\), \(O(N!)\). Examples of algorithms for each of these.
  2. Memory Management in C++
    1. Correct usage of dynamic memory operators: new and delete for simple objects and arrays.
    2. Implementation of class destructors.
    3. Diagnosing and correcting memory errors (segmentation faults and memory leaks) using Valgrind.
  3. Linked Lists. [STEPHENS] chapter 3.
    1. Singly linked list.
    2. Double linked list.
    3. Circular lists.
    4. Node insertion and deletion.
    5. Sequential traversal.
    6. Using sentinels.
    7. Advantages and disadvantages over arrays.
  4. Stacks and Queues. [STEPHENS] chapter 5.
    1. LIFO and FIFO methods.
    2. Inserting and removing elements.
    3. Implementions using linked lists, arrays, and circular arrays.
    4. Evaluation of expressions in postfix notation.
    5. Common examples.
  5. Sorting. [STEPHENS] chapter 6.
    1. \(O(N^2)\) algorithms (insertiosort, selectionsort, bubblesort).
    2. \(O(N \log N)\) algorithms (heapsort, quicksort, mergesort).
    3. Sub \(O(N \log N)\) algorithms (countingsort, bucketsort).
    4. C++ std::sort function using a custom comparator.
  6. Searching. [STEPHENS] chapter 7.
    1. Linear search.
    2. Binary search.
  7. Hash Tables. [STEPHENS] chapter 8.
    1. Hashing functions.
    2. Collisions.
    3. Chaining.
    4. Open addressing and linear probing.
  8. Recursion. [STEPHENS] chapter 9.
    1. Design and implementation of recursive algorithms.
    2. Base case and recursive step.
    3. Tracing recursive function execution using droid diagrams.
    4. Common examples.
  9. Binary Trees. [STEPHENS] chapter 10.
    1. Tree terminology and properties (ancestor, binary tree, binary search tree, branch, child, complete tree, degree, descendant, full tree, height, internal node, leaf node, level, parent, perfect tree, root, sibling, subtree).
    2. Searching and node insertion in a binary search tree.
    3. Tree traversal algorithms (inorder, preorder, postorder, levelorder).
    4. Design and implementation of tree operations.
    5. Expression trees.
  10. C++ Standard Containers (simple usage and basic operations).
    1. std::vector
    2. std::list
    3. std::stack
    4. std::queue
    5. std::set
    6. std::map
    7. std::unordered_set
    8. std::unordered_map

Allowed Items During the Exam

NOTE: Once the exam starts you are not allowed to share any items with anyone else.

  1. Pen, pencil, eraser, pencil sharpener.

  2. Simple scientific calculator. You are not allowed to use a cell phone, programmable calculator, tablet, computer, or any other electronic device.

  3. Personal cheat sheet with the following characteristics:

    1. Must be one of these:

      • \(5 \times 8\) inches index card (\(12.7 \times 20.32\) cm).

      • Half letter size sheet of paper (\(13.97 \times 21.59\) cm).

    2. Must be handwritten. Computer-made cards/sheets are not allowed.

    3. You can write in both sides of the card/sheet.

    4. Must include your student ID and full name on the upper-left corner of both sides of the card/sheet.

    5. There are no restrictions on the specific content written on the card/sheet.

Sample Questions

  1. Name an algorithm that has an \(O(\log N)\) complexity.
  2. You have three algorithms: A, B, and C. Algorithm A has a performance of \(O(N!)\). Algorithm B has a performance of \(O(N^3)\). Algorithm C has a performance of \(O(2^N)\). Which algorithm has the best performance? Which one has the worst?

  3. You have the following C++ code:

    #include <iostream>
    #include <vector>
    int f(int x)
        int r = 0;
        while (x) {
            x /= 2;
        return r;
    void g(int n, std::vector<int>& v)
        for (int i = 0; i < n; ++i) {

    What is the time complexity of function g? Explain your answer.

  4. Assume that the following code completes the program that started with the code from the previous question. What gets printed to the standard output when running the program?

    int main()
        std::vector<int> v;
        g(10, v);
        for (int x : v) {
            std::cout << x << " ";
        std::cout << "\n";
        return 0;
  5. When trying to find a certain item inside a linked list, which search algorithm should be used and what is its time complexity?

  6. This sorting algorithm uses the fairly obvious fact that, if an array is not sorted, it must contain two adjacent elements that are out of order. The algorithm repeatedly passes through the array, swapping items that are out of order, until it can't find any more swaps. What’s the name of this algorithm?

  7. What gets printed to the standard output when running the following C++ code?

    #include <algorithm>
    #include <iostream>
    #include <vector>
    bool comparator(int a, int b)
        return std::abs(a) <= std::abs(b);
    int main()
        std::vector<int> v {-35, 12, -43, 50, -77, -76, -8, 69};
        std::sort(v.begin(), v.end(), comparator);
        for (int x : v) {
            std::cout << x << " ";
        std::cout << "\n";
        return 0;
  8. You have the following hash function:

    $$ H(k) = k * 97 + 13 $$

    Given a hash table implemented using open addressing and linear probing on an integer array of size 10, indicate the final state of that table after inserting the following numbers using the hash function above:

    $$ 54, 45, 92, 22, 15, 90 $$
  9. In order to solve the Tower of Hanoi puzzle with ten disks, at least how many moves do you need?

  10. Build a binary search tree by inserting the following values:

    $$ 34, 15, 25, 40, 60, 51, 9, 31, 87, 71, 24 $$

    Traverse the resulting tree in inorder, preorder, postorder, and levelorder.

  11. Given the following postfix expression, what is the result of evaluating it?

    $$ 100 \; 4 \; 5 \; 6 \; + \; * \; - \; 15 \; 3 \; / \;+ $$
  12. Indicate the infix expression corresponding to the previous question and build the corresponding expression tree.

  13. You are given a binary tree with the following traversals:

    • Inorder: B A F D G C H E
    • Preorder: A B C D F G E H

    With this information rebuild the corresponding binary tree.

  14. You have a perfect binary tree with 511 nodes. What is the height of this tree?

  15. You have the following struct declaration that represents a binary tree node.

    struct Node {
        Node* left;
        Node* right;

    Write in C++ or pseudocode a recursive function called node_count that returns the total number of nodes contained in a given tree. The function’s signature is as follows:

    int node_count(Node* p)
  16. What gets printed to the standard output when running the following C++ code?

    #include <iostream>
    #include <queue>
    #include <stack>
    int main()
        std::stack<int> s;
        std::queue<int> q;
        while (not q.empty()) {
            int x = q.front();
            if (x % 2 == 0) {
        while (not s.empty()) {
            int x =;
            std::cout << x << " ";
        std::cout << "\n";
        return 0;