LMU ☀️ CMSI 2120
DATA STRUCTURES
Practice

Reinforcement Questions

Do you like spaced repetition learning? Have you used Anki or Quizlet? Whether or not spaced repetition works for you, periodically working on flash-card like questions can be a lot of fun, and just may help you retain information. Here are a few problems tied to the course material. Visit them periodically, and feel free to use them in your own spaced repetition learning practice!

  1. Why are data structures and algorithms always studied together?
  2. What is a data type and how does it differ from a data structure?
  3. Why is it said that data types are algebras?
    Because they really are. Wikipedia says “In universal algebra, an algebra (or algebraic structure) is a set A together with a collection of operations on A.” A data type is indeed a set of values with a collection of operations on those values.
  4. Give pictorial representation (using the notation from the course notes on data structures and algorithms) for Quaternions.
    Quaternion Algebra
  5. What are the three building blocks of data structures?
    Arrays, Objects, Links.
  6. How do objects differ from arrays?
  7. Can an object be nested inside of an object?
  8. Can an object be nested inside of itself?
  9. Draw a picture of an object representing your favorite movie. Include the title, the release date, the director names (as an array of strings), the MPAA rating, and the major cast members (as an array of objects, where each object holds the actor name and the character name).
  10. Draw a picture of the “tree of objects” representing the expression 95 * x + y. Some of the objects in the tree will be nodes with fields operator, left, and right; others will have either a variable name or a numeric value.
  11. What are the four major data structure topologies and how are they characterized? You can use pictures in your answer.
  12. What are the names of the command line programs that (a) compile Java programs, (b) run Java programs, and (c) provide a REPL for Java fragment execution?
    (a) javac (b) java (c) jshell.
  13. To run a Java app, what must exist?
    A class with a public static void method named main with a single array-of-strings parameter.
  14. How do you print a line of one asterisk followed by a line of two asterisks followed by a line of three asterisks and so on, finishing with a line of 100 asterisks, using a for-statement in Java?
    for (var i = 1; i <= 100; i++) {
        System.out.println("*".repeat(i));
    }
    
  15. How do you make a Java variable unable to be reassigned?
  16. How does Java syntactically differentiate single-line and multi-line strings?
  17. What exception should you throw in Java when a method receives an argument that does not make sense?
  18. What is the most significant difference between the lists List.of(20, 50, 80) and new ArrayList<Integer>(20, 50, 80)?
    The former is immutable and the latter is mutable.
  19. How can you make an immutable map in Java with the keys "A", "B", and "C" mapped to 1, 2, and 5 respectively?
  20. How does Java evaluate the expression 2000000000 + 2000000000?
  21. Java BigIntegers cannot be multiplied with the * operator. What do you use instead?
  22. In Java, does the expression s.stripTrailing() actually remove blanks from the string referenced by variable s? If not, what does it do, exactly?
  23. When should you use a record instead of a general class in Java?
  24. What is a Java interface?
    Something that describes a behavior that in general can be implemented in many different ways.
  25. What are the three kinds of methods that a Java interface can have?
    abstract, default, static.
  26. Are stacks LIFO or FIFO?
  27. What do push and pop do, in a stack?
  28. When making a linked stack, can the node objects be immutable?
  29. Are queues LIFO or FIFO?
  30. What do enqueue and dequeue do, in a queue?
  31. Why are linked queues generally represented as circular structures? That is, what problems might you face if you implemented them with a non-circular approach?
  32. Why are array-based queues generally represented as circular structures? That is, what problems might you face if you implemented them with a non-circular approach?
  33. What are the basic operations of an immutable list data type?
  34. What are the basic operations of an mutable list data type?
  35. In what sense is a stack a restricted kind of a list?
  36. In what sense is a queue a restricted kind of a list?
  37. Why are mutable lists often represented as doubly-linked structures?
  38. Why do mutable, doubly-linked lists use a header node?
  39. Why can immutable lists get away with being represented with singly-linked structures?
  40. In a list, what do we call the (a) first element, and (b) the sublist of a list containing all but the first element, of a list?
  41. What is, roughly, the log-base-two of a billion?
  42. What is $2^{20}$, exactly?
  43. What is a time complexity function?
  44. What big questions are we trying to answer when doing asymptotic complexity analysis?
  45. Why do we throw away leading constants and smaller terms when writing $O$, $\Theta$, and $\Omega$ expressions?
  46. Express each of the following in $\Theta$-notation: (a) constant time, (b) logarithmic time, (c) linear time, (d) linearithmic time, (e) quadratic time, (f) cubic time, (g) polynomial time, (h) exponential time, (i) factorial time.
  47. If the input size of a function with time complexity $\Theta(n^3)$ doubles, what happens to the running time?
  48. What is recursion?
  49. Give a recursive definition of a palindrome.
  50. What is the difference between a recursive definition and a circular definition?
  51. Define the set of triangular numbers recursively.
  52. Define (a) a recursive algorithm and (b) a recursive data structure.
  53. What is the difference in space complexity between a recursive factorial function and a recursive gcd function?
  54. What is tail recursion?
  55. How can we make recursive functions with “overlapping” invocations more efficient?
  56. Name some $\Theta(n^2)$ sorting algorithms.
  57. Name some $\Theta(n \log n)$ sorting algorithms.
  58. Name some $\Theta(n)$ sorting algorithms.
  59. Explain how the Counting Sort Algorithm works.
  60. Combsort is to Bubblesort as ________________ is to Insertion sort.
  61. Describe the best and worst case situations for Heapsort. That is, state which permutations of the input array result in the best and worst case running times for the algorithm.
  62. Describe the best and worst case situations for Treesort.
  63. Describe the best and worst case situations for Insertionsort. Why is its best case better than the best case for the other sorting algorithms you know? Why is its worst case worse than the worst case for the other algorithms?
  64. Why is Quicksort usually faster than Mergesort when the data to be sorted is stored in an array?
  65. Of which simpler sorting algorithms is Timsort a hybrid of?
  66. Of which simpler sorting algorithms is Introsort a hybrid of?
  67. Explain how Stoogesort works.
  68. Explain the difference between Bogosort and Bozosort.
  69. Explain precisely the difference between height and depth in the context of trees.
  70. Give general guidelines for choosing between a TreeSet and a HashSet.
  71. Explain how insertion into a heap-based priority queue works.
  72. Explain how deletion from a heap-based priority queue works.
  73. Both stacks and queues are a kind of priority queue. What exactly does this statement mean?
  74. Does the adjacency list representation of a graph have any advantages over other representations? If so, what are these advantages?
  75. Explain informally but precisely why the problem "Does graph $G$ have a vertex cover of size less than or equal to $k$?" is in NP.
  76. The problem "Does graph $G$ have a vertex cover of size greater than $k$?" has time complexity is $\Theta(1)$. Why, exactly?
  77. How many edges are there in the complete graph $K_n$?
  78. Traverse the following graph (a) breadth-first and (b) depth-first starting at node F:

    A — B — C — D — E — F — G — H — I — J

Design Problems

Some of these problems are pencil-and-paper sketches, and some ask you to write Java interfaces. They are here to improve you skills in data modeling, which is really, really important! Work on these with friends where possible.

  1. Give a UML class diagram for a part of a movie search application that keeps tracks of movies, actors, characters, directors, producers, release dates, songs (and their authors), and writers. You need only show the classes and their relationships (with cardinalities), though throwing in a few fields cannot hurt.
  2. Give a UML class diagram for a part of an HR database that keeps track of employees and their managers (who are also employees), the departments they work in, and the projects they work on. Employees have a name, birthday, salary, and zero or one manager. Employees can belong to one or two departments and can work on any number of projects, including zero. Projects have a budget and are run by a single department. In your diagram, show fields but not operations, and show all cardinalities. Pay attention to generalization, composition and aggregation.
  3. Draw a UML class diagram for basketball teams, their players (and their positions), their coaches, their schedules and their win-loss records.
  4. Write a Java interface for a robotic arm that controls an adjustable wrench. Be creative but not too unrealistic. You may assume the existence of classes named Point and Vector for 3-D positions and directions, respectively.
  5. Write a Java interface for a robotic arm that controls a flame thrower. Be creative but not too unrealistic. You may assume the existence of classes named Point and Vector for 3-D positions and directions, respectively.
  6. Write an interface for an immutable single-variable Polynomial data type. Make sure it has a fairly complete set of operations (degree, add, subtract, coefficient-for-given-exponent, evaluate-at-x, derivative).
  7. Write an interface for an mutable single-variable Polynomial data type. Make sure it has a fairly complete set of operations (degree, add, subtract, coefficient-for-given-exponent, evaluate-at-x, derivative).
  8. Write a Java interface for a sequence with the following behavior. At any time the sequence is in either read-only or write-only mode. The sequence must be opened before it can be used, and must be closed before it can be reopened again. The mode (read only or write only) can only be set when the sequence is opened. Reading is always done sequentially from the beginning of the sequence; there is no random access nor is there any backing up. Writing can only be done by adding new elements to the end of the sequence; there is no overwriting of elements. However the sequence can be completely erased. The sequence can, but does not have to be, constructed with a fixed capacity, so writing may cause an exception to be thrown if the sequence is full (though note that this is not the only possible exception that can be thrown).

Problems to Work Out

You might enjoy doing these on paper-and-pencil or whiteboard or tablet. You might have to do some programming or calculations on the side, but that’s ok!

  1. Given a Bounded Queue of capacity 4 which is initially empty, draw pictures of the queue after each of the following steps.
        enqueue a
        enqueue b
        enqueue c
        dequeue
        enqueue d
        dequeue
        enqueue e
        enqueue f
        dequeue
        dequeue
    
  2. (Courtesy of E. Harry) Show that $(\lambda n. 2n + 100\log n) \in O(n)$
    Choose $c = 102$ and $N = 1$. Then for every $n \geq 1$:

    $\begin{array}{lcl} |2n + 100\log n| & \leq & 2n + 100\log n \\ & \leq & 2n + 100 n \\ & \leq & 102n \end{array}$

  3. (Courtesy of E. Harry) Show that $(\lambda n. 5n^2 + 3n\log n +2n+5) \in O(n^2)$
    Choose $c = 15$ and $N = 1$. Then for every $n \geq 1$:

    $\begin{array}{lcl} |5n^2 + 3n\log n +2n+5| & \leq & 5n^2 + 3n\log n +2n+5 \\ & \leq & 5n^2 + 3n^2 +2n^2 + 5n^2 \\ & \leq & 15n^2 \end{array}$

  4. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    int x = 1;
    for (var i = 0; i < n; i++) {
        for (var j = 1; j <= x; j++) {
            System.out.println("*");
        }
        x = x + x;
    }
    
  5. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 0; i < n; i++) {
        for (var j = 0; j < i; i *= 2) {
            System.out.println("*");
        }
    }
    
  6. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i * i <= n; i++) {
        for (var j = 1; j <= n; j += j) {
            System.out.println("*");
        }
    }
    
  7. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i <= n; i *= 2)
        for (var j = 1; j <= n; j++)
            System.out.println("*");
        }
    }
    
  8. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i <= n; i *= 2)
        for (var j = 1; j <= i; j++)
            System.out.println("*");
        }
    }
    
  9. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i <= n * n; i++) {
        for (var j = 1; j <= n; j *= 2) {
            System.out.println("*");
        }
    }
    
  10. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i <= n; i++) {
        for (var j = 1; j <= i * i; j *= 2) {
            System.out.println("*");
        }
    }
    
  11. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = 1; i <= n; i++) {
        for (var j = 1; j <= i; j *= 2) {
            System.out.println("*");
        }
    }
    
  12. What is the time complexity of this code fragment? (Use $\Theta$-notation)
    for (var i = n; n >= 1; n /= 4) {
        for (var j = 0; j < i; j++) {
            System.out.println("*");
        }
    }
    
  13. Give both the best-case and worst-case time complexities of this code fragment. (Use $\Theta$-notation)
    // Assume some global variable t is defined out here
    for (var i = n; n >= 1; n /= 2) {
        if (new Date().getTime() > t) {
            for (var j = 0; j < i; j++) {
                System.out.print("*");
            }
        }
    }
    
  14. Give both the best-case and worst-case time complexities of this function. (Use $\Theta$-notation)
    public static long power(int x, int n) {
        return n == 0 ? 1
            : n % 2 == 0 ? power(x * x, n / 2)
            : x * power(x * x, n / 2)
    }
    
  15. What is the time complexity (in $\Theta$-notation) of a procedure to print out the exact value of $2^n$, where $n$ is a nonnegative integer? The procedure described is supposed to print a result no matter how large the result may be.
  16. An algorithm with time complexity $T(n) = n^3$ can process a 100-element list on our PC in 10 seconds.
    1. How long would it take to process a 200-element list?
    2. If we ran the algorithm on a machine that was 10 times faster than our PC, how large of a list could we process in 30 seconds?
    3. How much faster than our PC would a computer have to be in order to process a 1000000000-element list in a time span of 1 hour?
  17. An algorithm with complexity function T(n) = n log n processes a 64-element list in three minutes and 12 seconds on our PC.
    1. How long does it take to process a 128-element list?
    2. How large of a list could a computer that is 8 times faster than our PC process in 10 seconds?
    3. How much faster would a computer have to be than our PC to process a list of size 10 in a second?
  18. An algorithm of with time complexity function T, where T(n) = 2n log n, can process a 32 element list in 2 minutes and 40 seconds on our PC.
    1. How long would it take to process a 64 element list?
    2. If we ran the algorithm on a machine that was 4 times faster than our PC, how large of a list could we process in 16 seconds?
  19. An algorithm with complexity function T(n) = n2 processes a 100-element list in two minutes on our PC.
    1. How long does it take to process a 200-element list?
    2. How large of a list could a computer that is 2500 times faster than our PC process in a minute?
    3. How much faster would a computer have to be than our PC to process an element of size one billion in a second?
  20. An algorithm with complexity function $T(n) = 2^n$ can process a 50-node graph on our PC in one month.
    1. How long would it take to process a 75-node graph?
    2. If we ran the algorithm on a machine that was one billion times faster than our PC, how large of a graph could we process in one month?
    3. Using the computer in Part (b) above, could we process a 100-element graph in our lifetime? Why or why not?
  21. We have seen that there is little hope of solving problems of size 100 or so with algorithms of complexity $\Theta(2^n)$ even when billions of operations can be carried out per second. But what about algorithms of complexity $\Theta(1.1^n)$? How do these algorithms compete with quadratic algorithms? In particular, if a billion operations can be performed in one second, up to what problem size will the $\Theta(1.1^n)$ be faster than a $\Theta(n^2)$ algorithm?
  22. Give an example of an algorithm with worst-case complexity $\Theta(n^{3/2})$.
  23. Consider the three complexity classes $\Theta(log_2 n)$, $\Theta(\ln n)$, and $\Theta(log_{10} n)$. What is the difference between them, if any?
  24. Suppose you were given two lists of strings $(a_1, a_2, \ldots, a_n)$ and $(b_1, b_2, \ldots, b_n)$. Post's Correspondence Problem is to find a sequence of integers $\langle i_1, i_2, \ldots, i_k \rangle$, each from $\{1 \ldots n\}$, such that

    $a_{i_1}a_{i_2}\ldots a_{i_k} = b_{i_1}b_{i_2}\ldots b_{i_k}$

    For example, if $A$ = ("able", "eci", "gek", "foo", "d", "un") and $B$ = ("", "cidabl", "dab", "oo", "e", "und") then a solution is the sequence $\langle 6,5,2,5,1 \rangle$. It turns out that any algorithm to solve this problem has complexity $\Omega(\infty)$. What does this mean? Give a justification for this.

  25. Prove or disprove: $\lambda n. 3^n \in O(2^n)$.
  26. How many nodes are there on level $j$ of the binomial tree $B_k$?
  27. How many nodes are there in the binomial tree $B_k$?
  28. Draw the binary tree whose inorder traversal is E J F A D G H C B I, and whose preorder traversal is A E F J B C D G H I, or explain why no such tree exists.
  29. Draw the binary tree whose preorder traversal is E A S Y and whose inorder traversal is Y E S A, or explain why no such tree exists.
  30. Give an expression for the minimum height k-ary tree with $n$ nodes.
  31. If possible, draw the binary tree with postorder traversal H G F D C E A B and inorder traversal is C G H D F B E A, or state why no such tree can exist.
  32. Draw the binary tree with preorder A B C E D F and inorder A C E F D B or prove that no such tree exists.
  33. If the preorder and in order of a binary tree are the same, what does the tree look like?
  34. If the breadth-first and post order of a binary tree are the same, what does the tree look like?
  35. Write the mathematical expression for the number of nodes on the last two levels of a minimum height binary tree of n nodes.
  36. One way to represent a binary tree is as an array of records in which each record has three fields: one for the node data and two for the indices (into the array) of its two children.
    1. Draw the binary tree given by the following:
            ┌───────────┬────┬────┐
          1 │ Gamma     │  0 │  0 │
            ├───────────┼────┼────┤
          2 │ Beta      │  1 │  3 │
            ├───────────┼────┼────┤
          3 │ Epsilon   │  6 │  5 │
            ├───────────┼────┼────┤
          4 │ Omega     │  2 │  7 │
            ├───────────┼────┼────┤
          5 │ Sigma     │  0 │  0 │
            ├───────────┼────┼────┤
          6 │ Alpha     │  0 │  0 │
            ├───────────┼────┼────┤
          7 │ Tau       │  8 │  0 │
            ├───────────┼────┼────┤
          8 │ Delta     │  0 │  0 │
            └───────────┴────┴────┘
      
    2. Write a class (or type) declaration for this structure.
    3. How can you determine whether a given element in the tree is a leaf?
    4. How do you find the root of the tree?
  37. What is the height of a (binary) heap with $n$ elements?
  38. Draw the complete ternary tree of 26 nodes.
  39. In the array representation of a ternary tree, what is the index of the rightmost child of the node with index $i$? (Recall that the root has index 1.)
  40. Give a closed-form formula for the maximum number of empty spaces in the array representation of a $k$-ary tree of height $h$. Can you tell from the phrasing of this question that your answer should be a formula in terms of $k$ and $h$?
  41. Write the expression tree for the following Java expression
    (a = 3) >= m >= g . x [ 4 ] * ~ 6 || y %= 7 ^ 6 & p
    
  42. Give a sequence of five integers such that if each integer in the sequence is inserted, in order, into an initially empty splay tree, the resulting splay tree is completely degenerate. If this is not possible, state why it is impossible.
  43. Let $B$ be a binary search tree containing the numbers 1 through $n$. We may compute the number of possible input orderings (permutations) of these numbers that produce $B$, by counting the leaves of a tree $B'$ formed from $B$. Describe how to construct $B'$.
  44. Of the 3628800 possible insertion sequences of the numbers 1 through 10, how many of them produce the binary search tree (4 (2 (1) (3)) (7 (5 () (6)) (9 (8) (10))))?
  45. Draw all possible binary search trees containing the integers 1 through 4. How many such BSTs are there? (Hint: Remember what Catalan numbers are?) How many of these are AVL trees?
  46. Show the binary search tree that results from inserting, in order (into an initially empty tree), the values 20 6 4 8 15 22 11 13 14. Repeat for an AVL tree, a red-black tree, a splay tree, a $(2,3)$-tree, and a $(2,4)$-tree.
  47. Show the binary search tree that results from inserting, in order (into an initially empty tree), the values 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15. Repeat for an AVL tree, a red-black tree, a splay tree, a $(2,3)$-tree, and a $(2,4)$-tree.
  48. Show the binary search tree that results from inserting, in order (into an initially empty tree), the values 16 28 95 108 100 119 33 97 30 105. Repeat for an AVL tree, a red-black tree, a splay tree, a $(2,3)$-tree, and a $(2,4)$-tree.
  49. Show the binary search tree that results from inserting, in order (into an initially empty tree), the values 10, 90, 80, 20, -5, 1, -6, 6. Repeat for an AVL tree, a red-black tree, a splay tree, a $(2,3)$-tree, and a $(2,4)$-tree.
  50. Draw the tallest possible red-black tree with 17 nodes.
  51. Write (a) a recursive and (b) a non-recursive algorithm for finding the fewest number of nodes that an AVL tree of height $h$ may have.
  52. Is every full binary tree also an $(a,b)$-tree? If so, give the values for $a$ and $b$. If not, disprove.
  53. In the following tree, indicate for each node whether the first step in splaying it would be a zig, a zig-zig, a zig-zag, or none of the above.
                         R
                       /
                     N
                   /  \
                 K      P
               /   \      \
             E       L      Q
           /  \        \
         D      I        M
              /   \
            G       J
          /   \
        F       H
    
  54. Does the following function really determine whether or not a given simple, undirected graph has cycles? (Recall that a cycle in a simple undirected graph is a simple circuit whose length is greater than or equal to 3.) I mean, every time I draw up a graph with as many or more edges than nodes I find cycles. Am I missing something, or have I really found an $O(n)$ solution?
    public static boolean hasCycles(Graph g) {
        return g.numberOfEdges() >= g.numberOfNodes();
    }
    
  55. Give a highly-detailed pseudo-code description of a “nearly maximal” independent set for a given simple undirected graph, using a greedy algorithm. Then give an example of a graph for which your algorithm gives a sub-optimal answer.
  56. If you had an algorithm to solve the traveling salesperson problem you could use it to determine whether or not a graph had a Hamilton Circuit. How?
  57. If you were given the problem to display all solutions to the 8-queens problem you would have to examine and test all possible configurations.
    1. How many configurations are there?
    2. If you could examine one billion configurations per second, how long would it take you (at least) to find all solutions to the 30-queens problem?
  58. What is the chromatic number of a full ternary tree of height $n$?
  59. Give the (time) complexity of Prim’s algorithm for finding the minimum spanning tree of a graph, assuming that the priority queue is implemented as a heap.
  60. Is a Hamilton Path guaranteed to exist in a simple, undirected graph whenever the number of edges exceeds twice the number of nodes? Why or why not?
  61. Determining whether a simple, undirected graph is 3-colorable is $\mathit{NP}$-complete, but determining whether it is 2-colorable is in $P$. Give an $\Theta(e)$ algorithm for determining 2-colorability.
  62. Note that, for a simple, undirected graph with $n$ nodes, the $n \times n$ adjacency matrix $M$ is space-inefficient.
    1. Exactly how many entries in the matrix $M$ are actually required to represent the graph?
    2. To save space we could store the significant entries of $M$ in a one-dimensional array $A$. We would then need a function $f$ such that $M(i,j) = A(f(i,j))$. Write the closed-form expression for $f$. (Assume that nodes are indexed from $1 \ldots n$.)
  63. Suppose you were given (1) an algorithm to compute the complement of a simple, undirected graph and (2) an algorithm to find the maximum clique in a simple, undirected graph. How could you then compute the smallest vertex cover?
  64. Suppose you were given an algorithm to determine for some value $n$, whether or not a simple, undirected graph $G$ had a spanning tree in which no node had degree larger than $n$. Show how to use this algorithm to determine whether or not a graph has a Hamilton Path.
  65. The complement of a simple, undirected graph $G = (V,E)$ is the graph $G^C = (V,E')$ where $E' = \{(u,v) \mid (u,v) \not\in E\}$. Less formally, $G^C$ contains exactly those edges that are not in $G$.
    1. Is it easier to compute the complement of a graph under an adjacency matrix or an adjacency list representation?
    2. Write a method of a SimpleUndirectedGraph class that returns its complement.
    3. What is the (time) complexity of your algorithm?

Programming Problems

Programming practice is essential.

Small Java Apps

Here are a few short Java apps for practice. They all feel like the early apps from the Beginner Java Bootcamp.

  1. Write a Java app that simply prints today’s date (in the current time zone)
  2. Write a Java app that determines the probability that in a randomly selected group of 23 people, at least two people share a birthday. (Hint: this is more easily done by first computing the probability that no two people in the group share a birthday, then subtracting this value from 1.)
  3. Repeat the previous problem but rather than hard coding 23, get the group size as a command line argument.
  4. Write a Java app to average its command line arguments, or to prompt the user for a file name containing numbers to average if no command line arguments are given.
  5. TODO MANY MORE

Simple Java Functions

For each of the following, write the desired method as if it were to be executed in JShell.

  1. Write a function to convert all characters in a string to uppercase.
  2. Write a recursive function that takes in an integer $n$ and returns the largest integer less than or equal to $\log_2 n$.
  3. Write a recursive function that takes in an integer $n$ and returns the largest integer less than or equal to $\log_3 n$.
  4. Write a recursive function to return the number of digits in a natural number. Actually write the function recursively; don't use any shortcuts like Integer.toString(n).length()
  5. Write a function that takes in an integer $n$ and returns whether the string that denotes the base 10 value of $n$ is a palindrome. For example, the function would return true for the arguments 2992, 3, and 32023, but false for 104 and 31133.
  6. Addition is defined by repeating the increment operation, multiplication by repeating addition, exponentiation by repeating multiplication, and so on. Write function for the next operator in this sequence (sometimes called tetration).
  7. Write a recursive method to compute $x^n$, where $x$ is a BigInteger and $n$ is an integer. Your algorithm should take about $\log n$ computational steps, not $n-1$ steps.
  8. Consider the following method, which displays all possible strings of a given length from a given alphabet to standard output.
    public static void showStrings(char[] alphabet, int length) {
        show("", alphabet, length);
    }
    
    For example, the call showStrings(new char[]{'a', 'b', 'c'}, 2) displays:
    aa
    ab
    ac
    ba
    bb
    bc
    ca
    cb
    cc
    
    Write the helper method show recursively.
  9. Write a method which takes in a string, which is the name of a file, and a character, and returns the number of occurrences of that character in the file. Make sure to handle all errors — for example, if the file was opened, then it had better get closed before your method returns (for whatever reason).
  10. Write a recursive function to determine whether or not the items in an array of numbers appear in increasing order.
  11. Write a function that takes in a list (or array) of integers $v$, and an integer $n$, and returns whether or not there is a subset of the elements in $v$ which sums to $n$. In this problem the sum of the empty set of integers is 0.
  12. Write a Java method that takes in an array of integers $a$ and an integer $k$, whose value must be a legal index of $a$, and swaps the two subarrays $a[0..k-1]$ and $a[k..a.\mathtt{length}-1]$. For example if you input the array [8, 4, 5, 2, 9, 1, 7, 6] and the integer 3 then your method should modify the array to be [2, 9, 1, 7, 6, 8, 4, 5].
  13. Write a Java method that takes in an array of ints $a$, an int $x$, and an integer binary operator $f$, and returns the list obtained by applying $f$ to $x$ and each element of the array in order. For example, if you had:
    int[] a = new int[]{5, 7, 6, 1};
    
    and you called your method like this:
    whateverYouCallTheMethod(a, 2, (x, y) -> x - y));
    
    then your method should return the array [-3, -5, -4, 1].
  14. Write a recursive function to determine whether a given array is sorted. Then write it non-recursively.
  15. Write a utility method that takes a java.util.List and returns a new list made by duplicating each element in the original as in this example: stutter([A,B,C]) = [A,A,B,B,C,C].
  16. Write a utility method that interleaves two java.util.List objects. That is, interleave([A,Z,G,Q], [P, X, R]) would return a new list [A, P, Z, X, G, R, Q].
  17. Write a non-recursive function to determine whether or not a given element is a member of a singly-linked list.
  18. Write a Java method that takes an immutable singly linked list of nodes and returns a new list containing every other element of the original list. For example if your input list was ["dog", "car", "bat", "mop", "rug"] then your method should return ["dog", "bat", "rug"]. The result should contain new node objects.
  19. Write a method on a mutable doubly-linked list class for removing all the 0s from a list.
  20. Write a method on an immutable singly-linked list class for removing all the 0s—that is, return a new list like the input list, but with all zeros removed.
  21. Write a Java method that takes a mutable singly linked list and modifies the list by exchanging the first and second elements, the third and fourth elements, the fifth and sixth, etc., by moving references only! You may not change the data elements within nodes: you can only relink existing nodes in such a way that you get the desired result.
  22. Suppose you were given a mutable singly linked list of objects. Write a Java method to reverse this list by leaving all the objects in the nodes that contain them while moving links around.
  23. Give an $\Theta(n)$ function to sort an array of shorts in Java.
  24. Complete the ancestors and descendants method of the following class. Write them recursively.
    record Person(String name, Person mother, Person father) {
        public Set<Person> ancestors() {...}
        public Set<Person> descendants() {...}
    }
    

Not-So-Simple Functions

  1. Write a function to compute $f^k(n)$.
  2. Write a function to return the powerset of a set.
  3. Write a function to return the sum of subsets of a set of integers. For example the sum of subsets of the set $\{4,7\}$ is $0 + 4 + 7 + (4 + 7) = 22$.
  4. Write a non-recursive function to compute the number of leaves in a binary tree.
  5. Write a recursive instance method for a BinaryTree class that returns a collection of the data elements in the internal (non-leaf) nodes of a binary tree. For example, in the binary tree
                A  
              /   \
            B      C
           / \      \
          D   E      G
         /
        F
    
    your method needs to return the collection $[A, B, E, C]$.
  6. One of the nice features of Quicksort is that it allows a great deal of parallelism in an implementation. After partitioning, the slices on either side of the pivot can be sorted in parallel. Code up a parallel Quicksort implementation.
  7. Write a function that returns the list of prime factors of a given 32-bit integer. What is the complexity of your algorithm? Are you sure?
  8. Write a function that returns the list of prime factors of a given unbounded integer. What is the complexity of your algorithm? Are you sure?
  9. Write a function to compute the smallest dominating set of a simple undirected graph.
  10. Write a function to compute the maximum clique of a simple undirected graph.
  11. Write a function to find the smallest independent set that is also a vertex cover. (The implementation should run in polynomial time.)

Classes From Scratch

  1. Write an immutable 2-D Point record class, with coordinates of type double. Your constructor should reject NaN values for each coordinate. Also write a unit test.
  2. Write a mutable 2-D line class, using the record from the previous problem. A line is described with a point and a direction (the number of radians counterclockwise from the positive x axis). Provide accessors for the point and direction, and allow mutation through methods that move and rotate. Include a method to return the distance between a point and a line, and a method to determine whether two lines are parallel. Override toString. Also write a unit test.
  3. Write a record class for rational numbers. Make sure the constructor normalizes the number so that the equals method will work as expected. Also write a unit test.
  4. Write an array-based implementation for mutable single-variable polynomials. Polynomials should be represented in an array of doubles, indexed by exponent. Also write a unit test.
  5. Write an array-based implementation for immutable single-variable polynomials. Polynomials should be represented in some object with type java.util.Map, where the keys are the exponents and the values are the coefficients. Also write a unit test.
  6. Assume someone has already written a record class called Ellipse, whose fields are both radii, and defined methods to return the circumference and the area. Write a Circle record subclass that extends the ellipse class (to leverage all the hard work that the ellipse class does) but still feels like a circle for users of the new circle class.
  7. Assume someone has already written a record class called Ellipse, whose fields are both radii, and defined methods to return the circumference and the area. Write a Cylinder record class that relies on the ellipse class to help it get its surface area and volume.
  8. Implement a queue class, from scratch, in which the queue contents are allocated in fixed-size chunks (that is, as a linked list of arrays).
  9. Implement a class for the following kind of sequence (you'll have to name it, too). An object of this class can be opened in either read-only or write-only mode. Items are read in order starting from the first element; though at any time you may reset the read pointer to the first element. Writing to the sequence is allowed only by appending items to the end; at any time you can erase all the items in the sequence (there is no way to selectively delete an item). A sequence must be closed before its mode can be changed. Provide operations to query the current size, the current mode, etc. Declare relevant exceptions. You may choose either an array-based or a linked representation.
  10. Implement a class for an Oriented Tree (that is, where nodes can have an unbounded number of children). The class must include methods to:
    1. return the list of ancestors of a node
    2. return the mirror image of a tree
    3. return the height of a tree
    4. remove all nodes from the tree that do not meet a given condition

Proofs

Don’t be scared! Proofs are like programs. In both proofs and programs, you know where you are starting and you know what you want to end up with. You just have to fill in the middle parts.

  1. A relation is irreflexive iff $\forall x. \neg R(x,x)$ and is asymmetric iff $\forall x y. R(x,y) \supset ~R(y,x)$. Prove that a transitive irreflexive relation is asymmetric.
  2. Prove by induction that the number of subsets of a set with $n$ elements is $2^n$.
  3. Prove that the number of permutations of a set of $n$ elements is $n!$.
  4. Prove that the running time of the standard build-heap algorithm for turning an array, in place, into a heap is $\Theta(n)$, where $n$ is the number of elements in the array.
  5. Prove that the sum of the degrees of the nodes in a simple, undirected graph is twice the number of edges in the graph.
  6. Prove that a graph is 2-colorable if and only if it has no odd-length circuits (König's Theorem).
  7. Prove that if a simple undirected graph $G$ is a tree then $G^C$ is either (1) connected or (2) consists of two blocks: one containing only one node and the other a clique of the remaining nodes.
  8. Use graph theory to prove that in any group of six people, there are either three people that all know each other or three mutual strangers. (Hint: talk about cliques and independent sets.)