Self-Review Questions

  1. True or False questions

    T/F 15 n + 100 n2 = O(n2)

    T/F 6 n log n + 4 n = O(n)

    T/F log(n2) + 4 log(log n) = O(log n)

    T/F 2 n1/2 + 34 = O(n2)

    T/F 3n + 100 n2 + n200 = O(2n)

  2. Is it true that any function which is O(log n) is also O(n)?

  3. Is it true that searching an array is asymptotically faster than searching a liniked list?

  4. You have a method foobar(X) that iterates over each element in a one-dimensional array X. What is the time complexity of the following code?
    String [ ][ ] str = new String[n][n];
    for(int k = 0; k < n; k++)
       foobar(str[k]);
    

  5. What is the runtime complexity of the following method?
    public Stack crazyStackMethod(int[][] a)
    {
       Stack stack = new Stack();
    
       for(int i = 0; i < a.length; i++)
       {
          for(int j = 0; j < a.length; j++)
          {
             Stack tempStack = new Stack();
             stack.push(a[i][j]);
             while(!stack.isEmpty())
             {
                tempStack.push(stack.pop());
             }
             stack = tempStack;
          }
       }
       return stack;
    }
    

  6. What is the best-case runtime complexity of searching an array?

  7. What is the average-case runtime complexity of searching an array?

  8. What does amortized cost mean?

  9. What is the amortized cost of push() on a dynamic array-based Stack?

  10. What is the worst-case cost of push() on a dynamic array-based Stack?

  11. An alternative to a standard deletion strategy is known as lazy deletion. When deleting elements from a singly linked list, we delete them logically but not physically. This is done by marking the node as deleted (using a boolean value). The numbers of deleted and not-deleted elements in the list are kept as part of the list. If at some point the number of deleted elements is equal to the number of notdeleted elements, we traverse the list actually deleting all of the "lazily deleted" elements. How does this strategy improve runtime complexity of deletion?

  12. Given a Stack class implemented using one FIFO Queue.
    public class Stack
    {
       private Queue Q = new Queue();
       public E pop()
       {
          int k = 1;
          while( k++ < Q.size() )Q.enqueue(Q.dequeue());
             return Q.dequeue();
       }
       public void push(E o)
       {
          Q.enqueue(o);
       }
    }

    If Q.enqueue(o) and Q.dequeue() takes O(1) time what is the worst-case runtime complexities of push(o) and pop()?


Victor S.Adamchik, CMU, 2009