CS 367: Answers to the Midterm Practice Problems

  1. Algorithms A and B are analyzed and are found to have worst-case running times that are O(N log N) and O(N**2), respectively. What is the time complexity (using the big-Oh notation) of a program that first executes algorithm A and then executes algorithm B?

    O(N**2)

    Explanation: T(n) <= c1 N log N + c2 N**2 <= c3 N**2

  2. Determine, for the typical algorithm that you use to perform calculations by hand, the running time to: (a) add two N-digit numbers, and (b) multiply two N-digit numbers.

    a) O(n)

    b) O(n**2)

  3. What is the result of the last operation in the following sequence of operations:
    	insert(4),  insert(8),  insert(1),  insert(6),  remove(),  remove()
    
    when applied to

    a stack: 1

    a queue: 8

    a priority queue: 4 (assuming smallest has highest priority)

  4. Write a recursive method, mult(m,n) that multiplies two positive integers, m and n, using only repeated addition. That is, to multiply m*n, your recursive method should add m, n times. Be sure to include the pre- and postconditions for your method.
        //precondition:  m>0, n>0
        //postcondition:  returns m*n
        public static int mult(int m, int n){
           if (n=1)
    	  return m;
           return m + mult(m,n-1);
        }
    

  5. Boris Bitdiddle decides he doesn't need to use a stack to check for properly balanced parentheses. Instead, he decides to keep three integers called parenCount, bracketCount, and braceCount. Each of these counts is initially set to zero. When scanning an input expression, if he encounters (, [, or {, he increments the respective parenCount, bracketCount, or braceCount by one. If he encounters ), ], or }, he decrements the respective parenCount, bracketCount, or braceCount by one. At the end of the input expression, if the three counts are zero, his program informs the user that all types of parentheses are properly balanced.

    Would you use Bitdiddles' program to check your input text? Explain why or why not. If there are input strings for which Bitdiddle's algorithm won't work, give examples of those input strings.

    No. The program incorrectly reports that the parentheses are properly balanced in the following string: { ( } ).

  6. Suppose you have a List ADT as implemented in the List.java code you used on assignments 2 and 3. That is, assume the List is implemented using a ListNode that has two fields, item and next. Assume that item is of type Comparable and that a toString method is defined in the class that implements Comparable.

    (a) Write a method, reversePrint(), that uses a Stack to print the items in the list in reverse order.

    Assume a Stack ADT is already implemented and that the Stack ADT has methods isEmpty(), Push(item) and Pop(), which respectively return a boolean, push and pop items of type Comparable.

    You shouldn't need to write more than 10 new lines of code.

    class List{
       ListNode Head;
    
       void reversePrint(){
          Stack S = new Stack();
          //solution inserted here:
          ListNode tmp = head;
          while(tmp != null){
    	 S.push(tmp.item);
             tmp = tmp.next;
          }
          while (!S.isEmpty())
    	 System.out.println(S.pop());  // assumes pop() returns the top object
       }
    

    (b) What is the time complexity of the above reversePrint method (using the big-Oh notation)?

    O(n)